tags:

views:

437

answers:

5
+3  Q: 

Heap error in C

I know this is really general, but I get "this" (see below) when I run my .c file in Visual C++ 2008 Express. It happens when I call malloc (). Take my work on this - I dynamically allocate memory properly.

HEAP[Code.exe]: HEAP: Free Heap block 211a10 modified at 211af8 after it was freed Windows has triggered a breakpoint in Code.exe.

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

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

The output window may have more diagnostic information.

Why do I get this error? What does this even mean?

+4  A: 

The error message tells you exactly why you got it:

Free Heap block 211a10 modified at 211af8 after it was freed

You had a heap allocated block that was freed then something wrote to that area of memory. It's not nice to write to a freed block of memory.

Michael Burr
But I didn't use `free()` at all before this. What is a heap allocated block? where can I find "211a10"? And "211af8"?
anonymous
If you dynamically allocated memory, and didn't free it, then you weren't doing it properly.
Anon.
@anonymous: A heap-allocated block is one that's dynamically allocated (e.g. with `malloc`). You're positive you don't call `free` anywhere before this? Are you using any third-party libraries that allocate/deallocate memory? Or maybe you're allocating memory and are accidentally scribbling outside its bounds?
jamesdlin
@anonymous: "211a10" and "211af8" are addresses - if you look at the contents of memory at those addresses it might give you a clue what's writing to that location improperly. You might not be directly allocating memory yourself - library routines or other funcitons might be performing the allocation (and freeing).
Michael Burr
@anonymous - you say you haven't called `free()` yet at this point? It's possible that you may be writing outside of your allocated memory block(s) before you free anything (which is corrupting the heap, but resulting in a somewhat misleading error message).
Michael Burr
@jamesdlin, I was "scibbling outside of its bounds" earlier in the program. How do I credit you if you wrote in a comment?
anonymous
Ah Michael Burr, you're correct too, that's exactly what I was doing.
anonymous
A: 

Take my work on this - I dynamically allocate memory properly.

But are you sure your buffers are all of the correct size and you free() them properly? Double frees and buffer overflows can easily lead to heap corruption that can cause malloc() to fail in all kind of ways.

If the management structures used internally by malloc() get damaged, it will usually not lead to an error immediately. But later calls to malloc() or free() that try to use these damaged structures will fail do erratic things.

sth
+2  A: 

I dynamically allocate memory properly.

I think that the problem here is that you unallocate the memory inproperly. What I mean by this is that, you might be trying to use freed memory. Sorry I can't help any further, you could probably add the actual code.

Anzurio
A: 

Are you using malloc() on an array? Because I think the error just might be you forgetting to allocate an extra memory location at the end -- what happens is it tries to write to that location, which isn't allocated to it, and assumes it's trying to write to a place that has already been freed.

Vanwaril
A: 

The error isn't actually happening when you call malloc; that's just when it triggers a free heap scan. The actual error happened somewhere before. You malloced some memory at address 211a10 (that's what malloc returned to you). Then you (or some other lib) freed it. Then later, when you call malloc in debug mode, it scans the whole heap -- as a courtesy to you, the poor programmer. It discovers that somebody (your or some lib you call) wrote over part of that array, specifically at address 211af8, or 0xe8 bytes into the array. So you're either still hanging onto a pointer that's been freed (most likely) and using it, or you're just trashing random memory.

garyo