Exactly when the stack is unwound during exception

This is a discussion on Exactly when the stack is unwound during exception within the C++ Programming forums, part of the General Programming Boards category; I always thought that the stack was supposed to be unwound when the catch block is finished , after an ...

  1. #1
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115

    Exactly when the stack is unwound during exception

    I always thought that the stack was supposed to be unwound when the catch block is finished, after an exception. The long experience I've had with exceptions certainly supports this. A recent experience says otherwise. The code I used can be abstracted this way:
    Code:
    try {
       A_class_that_sets_a_global_pointer_to_null_in_its_destructor the_class;
       the_class.some_method_that_throws_an_exception();
    }
    catch(myexception &ex) {
       code_that_uses_above_mentioned_global_pointer();
    }//point at which I expected the stack to be unwound
    I had a segfault which Valgrind says was caused by that global pointer being set to null. However, as the name suggests, that pointer should only have been set to null after the_class was destroyed, and the_class should have only been destroyed once the catch block was finished... or so I was told. Is that wrong, or should I be looking elsewhere in my code, because the_class's destructor can never precede code in the catch block?
    What does the standard really say? I'm using GNU here, but that shouldn't be germane to the standard.

    Edit: and you know what, maybe I'm not even using the term "unwind" correctly. I'm going back to reresearch the issue, and I'm starting to wonder if I was wrong to equate "stack unwinding" with "deallocation of objects on the stack"
    Last edited by Angus; 10-16-2009 at 02:34 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Anything created in the scope of the try block will be destroyed before the catch block is executed.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Quote Originally Posted by bithub View Post
    Anything created in the scope of the try block will be destroyed before the catch block is executed.
    You are quite sure about this? Because I've been using exceptions for a long time. It may just be blind luck that I've never required destructors in the try block to be called, but this is new to me.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    The "stack unwinding", which includes deallocations and whatnot, takes place before the catch block is executed. Maybe this makes it more clear:

    Code:
    
    #include <iostream>
    #include <string>
    #include <stdexcept>
    
    struct foo
    {
    	foo( std::string const& name )
    	: name( name )
    	{
    		print( false );
    	}
    
    	void print( bool tilde )
    	{
    		std::cout << ( tilde ? "~" : "" ) 
    		<< name << "( )" << std::endl;
    	}
    	
    	virtual ~foo( void )
    	{
    		print( true );
    	}
    
    	std::string name;	
    };
    
    void print( std::string const& message )
    {
    	std::cout << message << std::endl;
    }
    
    int main( void )
    {
    	print( "start" );
    	foo f1( "f1" );
    	print( "entering a try block" );
    	try
    	{
    		foo f2( "f2" );
    		print( "entering a try block" );
    		try
    		{
    			foo f3( "f3" );
    			print( "throwing an exception" );
    			throw std::exception( );			
    		}
    		catch( ... )
    		{
    			print( "exception caught, now rethrowing" );
    			throw;
    		}		
    	}
    	catch( ... )
    	{
    		print( "exception caught" );
    	}
    	print( "done" );
    	return 0;
    }

  5. #5
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Ok, well I guess that's gotta be the way it is, then. But now that I think about it, whenever I see braces I instinctively keep away from anything inside them that's stack-based, from outside them. So in the past, when writing catch blocks, I wouldn't rely on anything in the try block, even thought I allowed myself to believe that such a thing was safe. After all, the code that alerted me to this wasn't written by me, it was written by a colleague, who, I guess, does not have the same instincts. Although, I was probably the one who lead him to believe that it was ok

    Thanks for your input

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    The fact that the stack unwinding occurs before the code in the catch block is run is a large part of what makes a throwing destructor so fatal.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 01:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

Tags for this Thread


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