What happens when the system throws an exception

Started by
4 comments, last by MessageBox 20 years, 7 months ago
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?
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.
GSACP: GameDev Society Against Crap PostingTo join: Put these lines in your signature and don't post crap!
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?
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"
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.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

This topic is closed to new replies.

Advertisement