But how? Im writing a logger class, which open file handle in constructor and close it in the destructor...it should be accessible at global scope. Oh and I'm avoiding those horrible singletons as you suggest, I dont like it either....
Normally when a process is killed, the OS should close all open handles, so I wouldn't worry much about the case when the application crashes. Plus, when the application crashes, you don't really have all that much control about what happens.
You usually see a crash when for example a segmentation fault (or anything else that will trigger a hardware interrupt) is caught by a handler, but unless you dereference a null pointer, you really don't know what has happened before that. Your application might have been writing 50 megabytes past the end of an array in an endless loop before eventually hitting a page that isn't writable. Which means, any data in your logger class including the handle value could already have been overwritten with rubbish, and you don't know. There's simply no way you can protect yourself against what happens in a crash.
In case of C++ exceptions, everything will be smooth anyway, it's just the hard crashes that will bug you, and like I said, not much you can do there really. Even installing a vectored exception handler won't be 100% safe since you cannot know what happened before your handler is called.
@japro: Thx man, I've already done that and it worked nicely. Im happy with that! @samoth: Well I know that, It's just Im trying to write a memory manager also, not just a logger class. OK now everything's smooth and nice, and Im sure there will be no more memory leak....
my logger normally opened and closed for every single log. so all i did was log("bla") and be done with it. worked very well, and allowed to open the log while the app was running (opened in the browser and f5ed all the way to see the actual state).
so no need for the global.
otherwise, you could have it in the logger class / namespace to be at least not in the public domain per se.
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
@dave: I ever did that too, the reason Im doing it this way is because I frequently write to log file (and should be fast), so open-close-ing file is no option (feel like shooting my own foot)
@Dave: Well it's not a problem, anyway Im logging specific engine log at the logger constructor and destructor. By making sure the logger's destructor called, I dont have to log those specific info (it's automatically called)... and Im writing a memory manager too. what starts should finishes, rite?