Let's talk about the wild pointer. Free heap block XXX modified at XXX after it was freed

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.