Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MessageBox

What happens when the system throws an exception

This topic is 5381 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

If, when you''re executing/debugging your progran and it halts and throws an exception is all the memory you''ve allocated on the heap up to that point, automatically deallocated or is it (I don''t want to even think about this ) gone for ever?

Share this post


Link to post
Share on other sites
Advertisement
the memory is not gone forever. depends on the OS. At the least, the memory will be taken back when the OS is restarted (The OS basically says "okay, i''m shutting down, i''m taking all your memory back no matter whether you''re done or not"). Some OS'' will be able to get the memory back when the exception is thrown and the program exits (Windows XP does it quite well i find). Don''t worry, there''s no way you can permanetly lose memory from having an exception thrown.

Share this post


Link to post
Share on other sites
So, if the program throws an exception, it''s actually not as bad as the program running properly but accidentally forgetting to deallocate memory at the end of the program?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by MessageBox
If, when you''re executing/debugging your progran and it halts and throws an exception is all the memory you''ve allocated on the heap up to that point, automatically deallocated or is it (I don''t want to even think about this ) gone for ever?




Well, when the exception is thrown, the program will trace back through the stack of all the calling functions until it encounters a catch block that handles the exception.

If no function handles the exception, then the program is halted:

In this example, the -> means "calls"

Main()->MainLoop()->processClick()->readXML("invalid node"->IXMLDOMNodePtr.GetNodeName()->throw(e) 


When the exception is thrown, it will trace back up the stack looking for a try/catch block. Because it''s an exception, the OS is one stackframe above the Main() and it deallocates the memory and closes the program as a "catch all"

Share this post


Link to post
Share on other sites
Here''s a description of what happens on Windows paraphrased from an msdn article by Matt Pietrek

Raising an exception causes the exception dispatcher to go through the following search for an exception handler:

The system first attempts to notify the process''s debugger, if any.
If the process is not being debugged, or if the associated debugger does not handle the exception,
the system attempts to locate a frame-based exception handler
by searching the stack frames of the thread in which the exception occurred.
The system searches the current stack frame first, then proceeds backward through preceding stack frames.
If no frame-based handler can be found, or no frame-based handler handles the exception,
the system makes a second attempt to notify the process''s debugger.
If the process is not being debugged, or if the associated debugger does not handle the exception,
the system provides default handling based on the exception type.
For most exceptions, the default action is to call the ExitProcess function.

Share this post


Link to post
Share on other sites
It''s up to the platform. Most platforms in use today will effectively reclaim the memory (when the process''s address space is torn down, if not before) causing no net leakage. This isn''t guaranteed, of course, but it''s fairly typical. Other options include leaking the memory until reboot.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!