联系
Knight's Tale » 技术

Error: Memory crash _BLOCK_TYPE_IS_VALID(pHead -> nBlockUse)

2010-06-10 00:12

The memory management in C++ is really a headache.

This evening I run my program, and get error: "Memory crash _BLOCK_TYPE_IS_VALID(pHead -> nBlockUse)" at runtime.  This error may be caused by several reasons:

1.  Write buffer overflow[1].

// allocate 10 bytes
char* ptr = new char[10];

// write to the 11th. byte ptr[10] = 0;

You wrote outside of the allocated range, and the app will most likely crash.

  1. You delete a memory block more than once[2].

You may new one memory segment, and you have one pointer pointed to this memory segment. And you assign another pointer to this pointer. So, the tragedy may happen when you want to delete this memory segment with both pointers. A simple example can be :

int a = new int[10];
int b = a;
delete[] a;
delete[] b;

Reference:

[1]. http://www.codeguru.com/forum/showthread.php?t=375768 [2]. http://www.dreamincode.net/forums/topic/53920-memory-crash-block-type-is-validphead-%26gt%3B-nblockuse/