Destructor called after exception?

This is a discussion on Destructor called after exception? within the C++ Programming forums, part of the General Programming Boards category; Hi, I'd like to determine, whether the destructor of an object was called during stack unwinding, after an exception was ...

  1. #1
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463

    Question Destructor called after exception?

    Hi,

    I'd like to determine, whether the destructor of an object was called during stack unwinding, after an exception was caught, or just the objects lifetime ended normally.

    std::uncaught_exception() would be the standard solution, unfortunately it does not work (always returns false) in VC6 (SP5).

    Below the contents of uncaught.cpp:
    Code:
    #include <exception>
    _STD_BEGIN
    
    _CRTIMP2 bool __cdecl uncaught_exception()
    	{	// report if handling a throw
    	return (false);
    	}
    
    _STD_END
    Funny stuff

    Any alternatives, ideas?

    Thanks in advance!
    Last edited by Carlos; 11-24-2003 at 09:17 AM.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Personally, if I wanted to see if a destrutor was called, I'd put a call to OutputDebugString in the destructor code and run the prog through VC++ (or any other windows based) debugger

  3. #3
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by Fordy
    Personally, if I wanted to see if a destrutor was called
    This is not my intention.
    Maybe I wasn't clear enough, sorry : I wrote a class (CallStack) which traces the call stack.

    I need to know whether ~CallStack is called due to an exception, if so, output the callstack to the log file / stdout:
    Code:
    CallStack::~CallStack()
    {	
    	/*if( uncaught_exception() )
    	{
    		Dump();		
    	}*/
    
    	if( !myStackList.empty() ) 
    	{
    		myStackList.pop_back();
    	}
    	myStackDepth--;
    }
    I had to comment out those lines, as they are useless (see my thread starter).

    Now let me explain the problem.
    The CallStack object is instanciated through a macro in each function.

    My problem is that I *have* to insert in the source in every catch block the call to dump() (though the DUMP macro ), which is ugly...
    Code:
    void donuts()
    {	
    	TRACE_FN(donuts); // instanciate CallStack object
    	try
    	{
    		const int a = 0;		
    		int i = i/a; // division by zero - exception is thrown 
    	}
    	catch(...)
    	{
    		DUMP; // dumping the CallStack
    		cout << "Exception caught!" << "\r\n";
    		cout.flush();
    		return;
    	}
    }

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Oh right - http://msdn.microsoft.com/library/de...rdbehavior.asp

    Looks like it is still an issue with the most recent versions!

    I guess you could look at the asm produced by VC++'s try/catch declarations and see if you can figure out a way to ascertain if an exception is being processed....might be lucky, might not - even then you would most likely be tied into that compiler

  5. #5
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by Fordy
    Oh right - http://msdn.microsoft.com/library/de...rdbehavior.asp

    Looks like it is still an issue with the most recent versions!
    Yes, it's a compiler problem. I've read somewhere that VC7 implements uncaught_exception() as required:
    (http://msdn.microsoft.com/library/de...texception.asp

    However, my code won't work under VC6 or prior versions.

    Therefore I have to keep my DUMP macro in every catch block, which is ugly, but at least it works...

    I tried to figure out other alternatives, no solution so far.
    Last edited by Carlos; 11-25-2003 at 05:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  3. Handle C++ exception and structured exception together
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2008, 09:21 PM
  4. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM
  5. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 01:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21