Jump to content
  • Advertisement
Sign in to follow this  
Bow_vernon

WinMain violates RAII (when code crashes)

This topic is 2878 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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??

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!