Emre Turkoz
Emre Turkoz

Reputation: 868

How to fix the heap error?

I'm running an iterative procedure in c++, and after the first iteration is completed as I expected, I get the following error beginning the second:

Heap block at 00000000212005D0 modified at 0000000021200674 past requested size of 94 Windows has triggered a breakpoint in myProject.exe.

This may be due to a corruption of the heap, which indicates a bug in myProject.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while myProject.exe has focus.

The output window may have more diagnostic information.

While showing this error, visual-c++ opens dbgheap.c, highlighting the section given:

extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
        const void * pUserData
        )
{
        if (!pUserData)
            return FALSE;

        if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
            return FALSE;

        return HeapValidate( _crtheap, 0, pHdr(pUserData) );
}

What might be the reason for this error?

Upvotes: 1

Views: 8459

Answers (2)

saurabh jindal
saurabh jindal

Reputation: 121

I would like to add that if you dont have access to purify, then valgrind is also a good tool and will most probably catch this error.

Also, with valgrind you dont need to build your project again (as is the case with purify). you can simply run your debug executable with valgrind and also valgrind is fast as well.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258568

That section is the method that checks the heap validity, it's not the source of your problem.

You're most likely dealing with a memory corruption, which can be hard to debug. Your best bet is to run a memory analyzer tool (such as Purify) or, if the codebase is small, look through it yourself, removing pieces until you find the source.

Of course, any kind of undefined behavior can lead to this. Most common sources are:

  • forgetting to return from a function that defines a return type
  • deleting objects multiple times
  • deleting derived objects through pointers to a base class with no virtual destructor
  • invalid use of casts (C-style casts, const_cast, reinterpret_cast)
  • accessing memory you don't own (writing beyond the bounds of allocated memory)
  • etc. (feel free to add here)

I'd start with a full rebuild though... you never know!

Upvotes: 4

Related Questions