Free heap block xxxxxx modified at xxxxxx after it was freed
I believe many of my friends have encountered the above problems, but they often suffer from the inability to locate the wrong code. In fact, this problem is generally caused by the use of a wild pointer.
So what is a wild pointer?
The wild pointer is different from the NULL pointer. The so-called wild pointer indicates that the memory indicated by the pointer has been recycled, and this pointer continues to be used, leading to undefined behavior.
For example:
Char * P = new char [512]; </P> <p> * P = '/0'; </P> <p> Delete [] P; </P> <p> * P = 'a'; // a wild pointer is displayed here. <br/>
To avoid such problems. The method of clearing pointer variables immediately after deletion is usually used.
Delete [] P; <br/> P = NULL; // null <br/> * P = 'a'; // The debugger will prompt you here, the error location is located.
This seems to be a good solution to the problem of wild pointers. In fact, it is not because you need to know that memory deletion is often out of touch with pointer variables.
For example:
Char * P = new char [512]; <br/> char * q = P; </P> <p> * P = '/0 '; </P> <p> Delete [] P; <br/> P = NULL; </P> <p> * q = 'a '; // Q and the memory deletion code are often not in the same location, so q = NULL cannot be specified; so a wild pointer still appears. <Br/>
How can we locate such problems?
The prompt information written at the beginning of the article will be used here.
Free heap block d49418 modified at d49574 after it was freed
After the heap block whose memory starts to 0xd49418 is released, the 0xd49574 position of the heap block's memory is changed.
In this way, we only need to find the object allocated to where 0xd49418 is located, and then locate the member variable 0xd49574.
Then, you can find all the code that uses the variable and modifies the variable content to locate the error location. Modify the code as needed to avoid modifying the memory after it is released.