Global object's destructor not called when code crashes

Started by
10 comments, last by markr 13 years, 3 months ago
However, if I put the object at main() scope, its destructor gets called even when crashing.
//this worksvoid main(){CBlah obj;}//this wont doCBlah obj;void main(){}

I need the destructor to be called(evn in a crash)but also to make the object accessible at the global scope..
Advertisement
best solution: find a way to not need it to be at global scope. globals are always a last resort.

oh, and don't just pack it into a singleton :) no lipstick-on-a-pig solutions.

just have it in main, and pass it around as needed.
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

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

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.
You could use a pointer in the global scope and set it to the address of an object that is created in the main...
@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

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

@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)
have you measured it to be a problem? it never was for me.
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

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

@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?

This topic is closed to new replies.

Advertisement