+ Reply to Thread
Results 1 to 2 of 2

Thread: What is "DAMAGE: after Normal block" error?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    43

    What is "Debug Assertion Failed!" error?

    Hi All,

    I have following code and it crashes with "Debug Assertion Failed!" error:

    Debug assertion dialog says something like this:

    "Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)"

    Code:
    class CStudent
    {
    private:
    	char* m_pName ;
    
    public:
    	CStudent()
    	{
    		m_pName = NULL ;
    	}
    
    	~CStudent()
    	{
    		if (m_pName != NULL)
    		{
    			delete []m_pName ;
    			m_pName = NULL ;
    		}
    	}
    
    	void SetName(char *pName)
    	{
    		if(pName != NULL)
    		{
    			if (m_pName != NULL)
    			{
    				delete []m_pName ;
    				m_pName = NULL ;
    			}
    
    			m_pName = new char[strlen(pName) + 1] ;
    			
    			if (m_pName != NULL)
    			{
    				strcpy(m_pName, pName) ;
    			}
    		}
    	}
    
    	const char* GetName()
    	{
    		return m_pName ;
    	}
    } ;
    
    void UsingStudent()
    {
    	CStudent objStudent1 ;
    	objStudent1.SetName("Mark") ;
    	OutputDebugString(objStudent1.GetName()) ;
    
    	CStudent objStudent2(objStudent1) ;
    
    	OutputDebugString(objStudent2.GetName()) ;
    }
    What is wrong with this code?

    Please help.
    Last edited by IAmADon; 05-26-2012 at 01:25 AM.

  2. #2
    You are missing custom copy constructor.

    Copy constructor supplied by the compiler does shallow copy. And on destructor both the object tries to delete the same pointer!

    So, you should overload the copy constructor and do the deep copy.

    Example:

    Code:
    class CStudent
    {
    private:
    	char* m_pName ;
    
    public:
    	CStudent()
    	{
    		m_pName = NULL ;
    	}
    
    	CStudent(const CStudent& objStudent) : m_pName(NULL)
    	{
    		if (objStudent.m_pName != NULL)
    		{
    			m_pName = new char[strlen(objStudent.m_pName) + 1] ;
    			strcpy(m_pName, objStudent.m_pName) ;
    		}
    	}
    	
    	~CStudent()
    	{
    		if (m_pName != NULL)
    		{
    			delete []m_pName ;
    			m_pName = NULL ;
    		}
    	}
    
    	void SetName(char *pName)
    	{
    		if(pName != NULL)
    		{
    			if (m_pName != NULL)
    			{
    				delete []m_pName ;
    				m_pName = NULL ;
    			}
    
    			m_pName = new char[strlen(pName) + 1] ;
    			
    			if (m_pName != NULL)
    			{
    				strcpy(m_pName, pName) ;
    			}
    		}
    	}
    
    	const char* GetName()
    	{
    		return m_pName ;
    	}
    } ;
    
    void UsingStudent()
    {
    	CStudent objStudent1 ;
    	char szData[100] = "Mark" ;
    	objStudent1.SetName(szData) ;
    	OutputDebugString(objStudent1.GetName()) ;
    
    	CStudent objStudent2 = objStudent1 ;
    
    	OutputDebugString(objStudent2.GetName()) ;
    }

+ Reply to Thread

Tags for this 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