WinMain violates RAII (when code crashes)

Started by
19 comments, last by owl 13 years, 3 months ago
For example:
int WinMain(/*blah u kno whut*/){CBlah obj;obj.Foo();/*crashes here*/return 0;}

the destructor of obj wont get called when code crashes, but in main(), it will. the thing is, Im making windows, not console app so I have to use WinMain. Care to advise me?
Advertisement
Subsystem (console or windows) and entry point (main or WinMain) are independent parameters. The default projects in Visual Studio defaults to main for the console subsystem and WinMain for the windows subsystem, but you can change these to whatever combination you want.

In Project Settings, Linker, System, you can change subsystem as you like. In Project Settings, Linker, Advanced you can change the entry point of your application; WinMainCRTStartup for WinMain and mainCRTStartup for main.
Note that when code crashes that means that you've invoked undefined behavior somewhere. This in turn means that any C++ guarantees about destruction of objects go completely out the window. Your compiler may have ways to force stack unwinding, such as MSVC's SEH mechanisms, but the behavior of any C++ mechanisms are completely coincidental and may not remain stable across builds or even runs of the program.
But weird enough, this doesnt happen if Im using main() as entry point (in console application, of course). In console program, any object defined within main() scope gets its destructor called when app crash. This is useful if I call cleanup code in its destructor, for example.
Oddly enough the fact that certain behavior is useful has absolutely nothing to do with whether or not you can depend on that behavior. You are trying to rely on undefined behavior and this can and will bite you in the rear.
Wow, SiCrane you really give me the creeps <shiver>. So it's undefined behaviour. Well do you know any trick to avoid memory leaks on program crash? some said that I should use try and catch clause. Ive never used that. What is that?? Can it call specific code when my application crashes??
Quote:Original post by Bow_vernon
Wow, SiCrane you really give me the creeps <shiver>. So it's undefined behaviour. Well do you know any trick to avoid memory leaks on program crash? some said that I should use try and catch clause. Ive never used that. What is that?? Can it call specific code when my application crashes??


Try/Catch are the keywords used to handle exceptions. Exceptions are the driving force behind RAII.
Quote:Original post by Bow_vernon
Well do you know any trick to avoid memory leaks on program crash?
That's kind of an ambulance at the bottom of a cliff don't you think?
Not to mention that anything not freed upon program termination is normally automatically reclaimed by the OS, so this cliff already has a massive air-bag at the bottom.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by iMalc
so this cliff already has a massive air-bag at the bottom.


Or at least a massive waste disposal unit to remove any remains that might have a negative effect on the productivity of other citizens.
Quote:Original post by iMalc
Not to mention that anything not freed upon program termination is normally automatically reclaimed by the OS, so this cliff already has a massive air-bag at the bottom.
For this reason, cleanup on termination is a non-issue almost everywhere.

If you have an underlying OS (Win/Mac/Linux, iPhone, etc.) then resources will be automatically reclaimed after a crash*. If you don't have an OS (i.e. an embedded platform), then you typically have to reboot the machine anyway after a crash, so leaked resources will simply cease to exist.

* there are a few resources that can't be reclaimed by the OS, notably Unix/Linux semaphores. However, if you are at the point of using semaphores, you probably know enough to handle errors correctly...

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement