+ Reply to Thread
Results 1 to 2 of 2

Thread: What is stack unwinding in C++?

  1. #1

    What is stack unwinding in C++?

    What is stack unwinding in C++?

  2. #2
    When an exception is thrown and control passes from a try block to a handler, the C++ run time calls destructors for all automatic objects constructed since the beginning of the try block. This process is called stack unwinding.

    Example;

    Code:
    class CStudent
    {
    private:
    	int m_nRoleID ;
    	char *m_pName ;
    
    public:
    	CStudent()
    	{
    		m_nRoleID = 0 ;
    		m_pName = NULL ;
    	}
    
    	CStudent(int nRoleID, char *pName)
    	{
    		m_nRoleID = nRoleID ;
    		m_pName = new char[100] ;
    
    		throw 1 ;
    		strcpy(m_pName, pName) ;
    	}
    
    	~CStudent()
    	{
    		if (NULL != m_pName)
    		{
    			delete m_pName ;
    			m_pName = NULL ;
    		}
    	}
    } ;
    
    void UseStackUnwinding()
    {
    	{
    		try
    		{
    			CStudent objStudent1 ;
    		}
    		catch(...)
    		{
    		}
    	}
    
    	{
    		try
    		{
    			CStudent objStudent2(1, "Max") ; //destructor will not execute, hence memory leak!
    		}
    		catch(...)
    		{
    		}
    	}
    }
    Note:
    For more information:
    http://publib.boulder.ibm.com/infoce...%2Fcplr155.htm

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts