Hi All,
When my code tries to free the memory allocated by the new operator it throws a debug error while using the delete operator...

CDocum* pDocum = new CDocum();
int nResult = pDocum -> QuoteDocProd();
delete pDocum;

Error Am getting is
Debug Error!
Damage after Normal Block(#298753) at (address is mentioned).

Recommended Answers

All 11 Replies

Can you show us the definition of CDocum::~CDocum and CDocum::QuoteDocProd?

What ever error you are getting it depends on CDocum::~CDocum because line 3 of your code internally calls CDocum::~CDocum. So as suggested by "m4ster r0shi" plz share definition of ~CDocum.

CDocum::~CDocum is empty,

{QuoteDocProd
m_bFromFrontEnd = FALSE;

    m_Date = sDate;                       // move values into member variables
    m_User = sUser;                
    m_Initials = sInit; 
    m_Group = sGroup;
    m_Generation = sGen;
return DocProduction();
}

Obviously, we need to see the definition of DocProduction too.

Is CDocum's destructor virtual? If yes, I suspect that the
problem is caused by accidentally overwriting the vtable.

_declspec(dllexport) CALLBACK CDocum::~CDocum
{
delete m_temp1;
delete m_temp2;
}

where both temp 1 and temp 2 are virtual.

virtual ~temp1();
virtual ~temp2();
temp2::~temp2()
{
    if (INFile.is_open())
        INFile.close();
    if (m_pBuffer)
        delete []m_pFileBuffer;
}
temp1::~temp1
{
     if (m_HOBuffer)
	delete []m_HOBuffer;
}

My best guess is that you delete[] memory that wasn't reserved with new[] .

Do the constructors of temp1 / temp2 set m_HOBuffer / m_pFileBuffer to zero?

Also, careful here:

temp2::~temp2()
{
    if (INFile.is_open())
        INFile.close();
    if (m_pBuffer) // either this should be m_pFileBuffer
        delete []m_pFileBuffer; // or this should be m_pBuffer
}

I just mis typed... both are m_pBuffer...

temp1::temp1
{
     m_HOBuffer = new char[1001];
}
temp2::temp2
{
      m_pBuffer = NULL
}

Also I have a doubt, if we use a getbuffer() function to allocate some memory and fail to use releasebuffer() function to release the extra memory what effects it might cause?

You mean CString::GetBuffer and CString::ReleaseBuffer ? I just checked the documentation on msdn...
(http://msdn.microsoft.com/en-us/library/aa314880%28v=vs.60%29.aspx, http://msdn.microsoft.com/en-us/library/aa300574%28v=vs.60%29.aspx)

...and it looks like you don't have to delete anything when you use CString::GetBuffer .

The buffer memory will be freed automatically when the CString object is destroyed.

So, if you delete[] memory you got with CString::GetBuffer , don't do it.

Also, note that CString::ReleaseBuffer does not deallocate the internal string buffer.

Use ReleaseBuffer to end use of a buffer allocated by GetBuffer.

No I just wanted to know about it... I have not tried to delete the getbuffer allocated memory.
In my code the debug error is thrown once the destructor frees all the memories and finally calls an instruction

call             operator delete (00504440)

This instruction generally gets executed whenever a destructor is called in all programs. Merely it is an inbuilt call at the end of destructor call...

and in debug window I Get

memory check error at 0x0803CD3C = 0x14, should be 0xFD.
memory check error at 0x0803CD3D = 0xCB, should be 0xFD.
memory check error at 0x0803CD3E = 0x4C, should be 0xFD.
memory check error at 0x0803CD3F = 0x5F, should be 0xFD.

Ok. First, just to verify that this is where the problem comes from, try commenting out delete m_temp1; and delete m_temp2; and see what happens. Then, if these
two are indeed the cause of the problem, it would help a lot if you showed us the parts
of the code that involve m_temp1 , m_temp2 , m_HOBuffer and m_pBuffer .

I found the error... in the declaration part one of the CString variable was declared and it was not used in any part of the code... when the destructor tried to delete that cstring variable it threw a debug assertion error... that CString variable was removed and the application debugged fine...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.