Archived

This topic is now archived and is closed to further replies.

TheMummy

Segmentation faults in stl::ofstrem

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 types


protected:
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 opt
Log 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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites