half-destroyed io streams dilemma

Started by
16 comments, last by shurcool 21 years, 8 months ago
cout in VC++ is implemented using the Meyer''s singleton (which is a static variable inside a function). That is prone to the destruction order problem. (See article "Singleton:The One" on gamedev)

I would consider this a flawed implementation of cout, but there is little we can do about it. In your case of the logging library, use the Nifty Counter technique (Again, see article)
Advertisement
quote:Original post by shurcool
...
so i guess the user will manually have to destroy the CLog class(es) (by calling a function, of course) before it reaches return 0; in main(), or what? can't use atexit(), can i?

Yep... your code does work. You are qualify to be a human compiler. ... using atexit() shouldn't have any problem.
Do you have your CLog .h and .cpp available for download?
... hmm... I'm in office... I'll look into it later... if not my boss willl munch me.



P/S: How is String class? You crafted it yourself?

[EDIT] Are you still using on your static cleaner?

[edited by - DerekSaw on July 31, 2002 11:03:16 PM]
"after many years of singularity, i'm still searching on the event horizon"
Actually, it (that simplified example 3 posts up) works fine printing to screen using cout. Replace it with an fstream, and only "Universe created" goes in the file. "Universe Destroyed" doesn't. That's what we're wondering about.

If it really is something to do with VC++, I guess we can't do much about it.

The static 'smart' cleaner does the same thing as atexit(), destroys the object when the program exits if it wasn't already. However, this also results in the log file being incomplete.

[edited by - JohnPC on July 31, 2002 11:39:19 PM]
quote:In your case of the logging library, use the Nifty Counter technique (Again, see article)


thanks, i will take a look. hopefully you''re right and it will work.

quote:... using atexit() shouldn''t have any problem.


as JohnPC already said, it does (with std::ofstream anyway).

quote:Do you have your CLog .h and .cpp available for download?


get it here.

quote:P/S: How is String class? You crafted it yourself?


typedef std::string String; // i might make my own class wrapper later

quote:Are you still using on your static cleaner?


yes, i am (just in case, but usually it''s not needed, since the Destroy function is called), JohnPC also covered this (i know he posted after you, so i''m just saying ).

once again, thanks to all for their help!

---
shurcool
wwdev
i read the article about singletons that Void suggested. i compiled the src for the nifty counter, and here''s the ouput i got:
Main INITMyClass CTORMethod calledMain EXITMethod calledPress any key to continue 

and yes, i''m SURE that it''s not breakmeyer.zip, but niftycounter.zip! the project is also called nifty counter, and in has a file called niftycounter.cpp or something. so i''m 100% positive.

anyway, i don''t see the MyClass dtor being called. :-\ that''s really weird... i mean, if the nifty counter allows mem leaks, then what good is it?

---
shurcool
wwdev
ok, i tried setting some breakpoints and found out that the dtor actually does get called. the reason i didn''t know is because when it''s called, the cout stream is already destroyed! so if i replace all cout''s w/ printf''s, the ouput is fine, and i see the "MyClass DTOR" message.

that proves that even this method is not working, so i guess the only solution is to manually call the Destroy() function of the CRoot class, which will destroy everything in the right order. not everything can be automatic, or so it seems.

once again, if anyone comes up w/ a better solution, please test it first (try using cout and std::ofstream in the dtor of your test class). because i''m pretty postive that there''s isn''t a better way, and i don''t want my time wasted on trying to get something to work if it''s not going to (not that i don''t appreciate the suggestions you guys offered).

thanks.

---
shurcool
wwdev
quote:Original post by shurcool
i read the article about singletons that Void suggested. i compiled the src for the nifty counter, and here''s the ouput i got:

snip

and yes, i''m SURE that it''s not breakmeyer.zip, but niftycounter.zip! the project is also called nifty counter, and in has a file called niftycounter.cpp or something. so i''m 100% positive.

anyway, i don''t see the MyClass dtor being called. :-\ that''s really weird... i mean, if the nifty counter allows mem leaks, then what good is it?


This is what I get from nifty counter.


Main INIT
MyClass CTOR
Method called
Main EXIT
Method called
MyClass DTOR
Press any key to continue


The nifty counter doesn''t leak. If so, that would be a waste of time. As I said, the problem is with VC++6 implementation of cout. I know why it works for me and not for you. I''m using STL library from www.dinkumware.com.
quote:Original post by Void
I''m using STL library from www.dinkumware.com.


for how much/where did u get it? and what do u think of STLport (which is free)?



---
shurcool
wwdev

This topic is closed to new replies.

Advertisement