Effective prevention methods for C + + memory leaks: Who uses, who deletes (1.2)

Source: Internet
Author: User

A memory leak is something new. Without delete, we can: the person who creates the dynamic object uses new to create the object, and the person who uses the object is responsible for freeing the memory block.


For example: I share a message queue with others, others put the message (new out of the object) in the message queue, he is not responsible for the release; I get a message from the message queue and once I get a message out of the message queue, there is no longer a message in the message queue, the control and the full power of the message are in my hands I should delete when I'm done with it. This ensures that the code is written in a simple way, only to be able to respect the week of the simple method to ensure that no memory leaks.


Another example: Ace, there is a ace_message_block, just see a variety of new, but did not see the delete, at that time to see the demonstration of the sample swelling is considered depressed, so he carefully delete, get the last few times repeatedly delete also put the program to hang up. Nima. It was just knowing that the tutorial was going to be released using the release method, and now the feeling should be understood. Because Ace_message_black uses the message count, it frees up memory with its own member method release method.


The above is just a sample of Message Queuing, and some scenarios are less typical. For example, it is possible for you to create your own use, release yourself, but just think about this principle, you can very easy from the business to find out when to release.



There's a Linux tool that can test your program for no memory leaks:Linux C + + memory leak Detection Tool and usage

Effective prevention methods for C + + memory leaks: Who uses, who deletes (1.2)

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.