Segmentation faults in stl::ofstrem

Started by
5 comments, last by TheMummy 22 years ago
I wrote a log file class, which makes use of the ofstream class. Whenever I compile the project with gcc 2.95.3 with these flags: -O3 -fomit-frame-pointer -Wall -march=i686 -malign-functions=4 -funroll-loops -fexpensive-optimizations -malign-double -fschedule-insns2 -mwide-multiply the systems receives a segmentation fault during runtime. If I compile without them, or if I remove all lines using ofstream, there is no problem. The debugger receives the segmentation fault in the destructor of the ofstream class.
Advertisement
can you post some of the code where you use the ofstream in your class?...
If you're inheriting, perhaps you should derive from ostream rather than ofstream. If you're just using an ofstream object, then I've no idea.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

[edited by - Kylotan on March 22, 2002 2:30:21 PM]
Here is the header file


      class Log{public:    Log(const char * filename="log.txt");    Log& operator <<(const char *c);    Log& operator <<(const int &c);    Log& operator <<(const float &c) ;    Log& operator <<(const char &c);    // ... further methods for other data typesprotected:    char filename[32];};extern Log logFile;  



I recently learned, that it is also possible to implement this as a kind of template method ...

The source:

  #include "log.h"#define optLog logFile;Log::Log(const char* filename){#ifndef opt    ofstream output(filename, 0x02 | 0x10);    output.close();    int i;    for(i=0; i<31 && filename[i]; i++) this->filename[i]=filename[i];    while(i<32) this->filename[i++]=0x00;#endif}Log& Log::operator<<(const char *c){#ifndef opt    ofstream output(filename, 0x02|0x04|0x08|0x20);    if(output.is_open ( )) output << c;    output.close();#endif    return *this;}// The other methods are all the same  


I added the ifdefs to compile with or without ofstream.



[edited by - TheMummy on March 22, 2002 2:59:37 PM]
try changing

if(output.is_open ( )) output << c;

to

if(output.is_open ( )) output.write((char*)(&(*c)), length);

//length should be the number of characters in the string that
//*c points to, not including the null terminator.
//do not use sizeof(c) or strlen(c) as they will not return the right number.
Have you allocated space for your filename? That''s some pretty freaky loop code there that looks like it could be replaced with a strcpy or two.
regarding freaky loop:
Yap ... I had some other problems with strcpy in other parts of the game, so I replaced strcpy with loops... Can''t remind anymore what it was ... The filename is allocated before the constructor is called, since the filename is a member variable of the Log class.

The Segmentation fault occurs in the destructor of the ofstream object. When I add an ofstream member object to the class, instead of creating an ofstream object at the beginning of every method, the Segmentation fault only occurs when the game exits - when the Log object gets destructed.

By the way I am using the GNU Standard C++ Library v3, and gcc 2.95.3. Maybe I should get the new one.

This topic is closed to new replies.

Advertisement