none
Heap Corruption Error in MFC RRS feed

  • Question

  • CString FileName = Dlg.GetPathName(); LoadProductionInfo(FileName.GetBuffer(FileName.GetLength()));

    Getting Heap Corruption Error.!

    Thank

    Tuesday, July 2, 2013 12:19 PM

Answers

  • I'm guessing the problem goes away if you comment out the LoadProductionInfo line?

    If so, then the problem is likely within that function.  You'll have to dig deeper and/or share more details about the code.

    The heap corruption is likely caused by one of these three things:

    1. A buffer overrun.  If you allocate an object on the heap, like new char[32], and you write 33 characters to that string, then you'll overwrite a byte on the heap somewhere that doesn't belong to you.  The compiler can't detect this flaw at compile time, so you'll have to detect it at run time.  Make sure you use safe functions that take size arguments for their output buffers.
    2. Modifying an object after it was deleted.  If you keep a pointer to an object that has been deleted, then use it to modify the memory where the object was stored on the heap, the memory has likely been repurposed and you can cause corruption to the heap.  The compiler can't detect that you are using a pointer to something that has been deleted.  The debugger can often help.  The debug version of your code will write out guard bytes (0xDD) to deleted memory.  If you see that an object's member variables have bizarre values that are 0xDDDDDDDD, then this is likely what has happened.
    3. Using uninitialized memory.  Failing to provide an initial value for something on the stack will surprisingly often cause this kind of error because they often take on the value of something that a previous function call was using.  Some of these errors are guarded against at runtime by in debug mode where the compiler will again write guard bytes (0xCC) to uninitialized memory so that it at least has a known value and you can spot it in the debugger.  The compiler often catches this kind of error though.

    Other forms of corruption are usually variations on a theme, such as bad index for an array (resulting in an overrun)  But in advanced cases, problems can be caused by multiple threads trying to access the same data.

    Tuesday, July 2, 2013 12:48 PM