caymard
caymard

Reputation: 188

Error from ntdll.dll because of a malloc (C++)

I met this error with my program, I never saw it before.

Here is the part of the code which is responsible :

double *xx;
xx = (double*) malloc(2*n*m*sizeof(double));

And the Visual Studio 2010 prompt text when debugging :

Windows has triggered a breakpoint in calib.exe.

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

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

The output window may have more diagnostic information.

I have a small experience in C++, for me it's a simple memory allocation, nothing more. What can be responsible of this error ? Few lines upper I also make a malloc for double, and no problems...

Thank ou for helping,

Clément

Upvotes: 4

Views: 7311

Answers (2)

harmic
harmic

Reputation: 30597

As both @Dewfy and @molbdnilo have stated, most likely the reason you are getting this error is not related to the call site, but something that has happened prior to this in your program.

malloc / free (and corresponding C++ new / delete) rely on the data structures in the heap to keep track of which memory has been allocated and which is free. This varies from one implementation to the next, but usually involves data structures at the beginning and/or end of each allocated area - typically linked lists etc.

The kinds of error which can corrupt these structures, and lead to this kind of error, include:

  • Writing data past the end of, or before the beginning of, an area that has been allocated
  • Calling free twice for the same memory
  • Attempting to free a block of memory that was not allocated in the first place (eg. calling free on a pointer half way into an allocated block)
  • Calling free on memory that was allocated via new or vice versa
  • Other stray pointer scenarios

Not all of the above would corrupt the heap and it is very dependent on the actual implementation. I am actually not that familiar with the Windows heap manager. Also the above list is not exhaustive.

Such problems are notoriously hard to find because the fault can occur quite some time after the problem code has executed.

If you were using a unix-like platform I would recommend the valgrind tool for finding this kind of problem. It looks as if there is some effort ongoing to port it to Windows, but it does not look to be complete. If your program is not windows specific and you have access to a Linux box you might want to give it a try there, and if you get a similar problem, try using Valgrind to catch it.

However I did find the 'Dr Memory' tool for windows which looks promising (although limited to 32 bit code).

BTW, the question is tagged C++ but you are using the C heap management functions. While you can use these in C++, it is preferred to use new and delete instead. But whatever you use, don't mix them - that would cause undefined behavior.

Upvotes: 4

Dewfy
Dewfy

Reputation: 23644

@molbdnilo - is right with his comment, just to diagnose problem use _heapchk function (example see http://msdn.microsoft.com/en-us/library/2xse74he.aspx) -right before call malloc - so you could ensure that heap is already corrupted (by the way what the value for n and m - did you initialize these?)

Upvotes: 1

Related Questions