Skip to main content
gamedev.net
🔒 Locked

destructors

Started by original vesoljc Dec 28, 2002 at 7:30 AM 5 replies 300+ views
Original Post
original vesoljc
original vesoljc
i set up a logging class (outputs formatted text to specifed text file) like this...
  
// header guard

class axpLogger
{
     axpLogger(char* filename, bool switch);
     ~axpLogger();

     void Out(char* fmtlist,...);
};

// constructor

...
// destructor

axpLogger::~axpLogger()
{
Out("* ");
}

void axpLogger::Out(char* fmt_list,...)
{
   // open file

   // parse through formmating list

   // write to file

   // close file

}

static axpLogger gLog("text.log",true);
// end header guard

  
when i compile and run this thing under MS VC6, i get three (3) "starts" at the end of file. but when i compile it under devc++ (g++) i get five (5) starts... shouldn''t there be only one? what''s going on? Abnormal behavior of abnormal brain makes me normal...
Abnormal behavior of abnormal brain makes me normal...
George2
George2
The definition of the global glog variable in the header, creates a glog for every .cpp file in which you include the header. How to solve this :
in one cpp file define the glog variable, and in the header of the log class declare the glog variable with extern e.g. :

extern apsLogger glog;
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
original vesoljc
original vesoljc
yeah, but shouldn''t static (create only one copy of it?) solve this?

Abnormal behavior of abnormal brain makes me normal...
Abnormal behavior of abnormal brain makes me normal...
George2
George2
Yes (I think, I never define variables with the static keyword outside of functions or classes) but, you have put it in the header file, thus it is defined in multiple compilation units (well if you include the header in multiple cpp files, which is the case). An #include statement just copy pastes the contents of the header in the current file, so if you define a variable in a header, that variable will exist in all cpp files that include the header (directly or indirectly), which in your case results in multiple glog''s and thus multiple call''s to the destructor of glog.

To avoid this kind of shit, just don''t define variables in headers.
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
original vesoljc
original vesoljc
well, if defined as extern, copilation under msvc gives me only one star (ok), but under g++ i still get two stars...

yeah, but everything is "header guarded", so multiple defines are not posible, or what?

Abnormal behavior of abnormal brain makes me normal...
Abnormal behavior of abnormal brain makes me normal...
SabreMan
SabreMan
quote:
Original post by original vesoljc
yeah, but shouldn''t static (create only one copy of it?) solve this?

No. File-scope static doesn''t mean "create only one", it means "internal linkage", which provides one per translation unit. I suggest that your code is somewhere making a copy of your object. You need to disable the copy ctor and assignment operator of your class.
cnstrnd
cnstrnd
imo your gLog should be a Singleton


    
#define gLog Singleton<apxLogger>::Instance()

...

void f()
{
gLog.Out( "My code rules" );
}



they have a simple yet very useful implementation
of such a template at http://www.boost.org

[edited by - cnstrnd on December 28, 2002 11:23:20 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.