Detailed analysis of Memory leakage detection

Source: Internet
Author: User

Some time ago, the course design was designed to detect memory leaks on Windows platforms. Next, we will extract some of them as Blog content for your reference in the future.

  Generally, memory leakage refers to heap memory leakage. Heap memory refersProgramMemory allocated from the heap must be explicitly released after use. C  ++  Use new and new [] to allocate a block of memory from the heap. after use, the program must call delete or delete [] to release the block. Otherwise, this memory cannot be used again, so we can say this memory is leaked.
The following four types of memory leaks summarize the most common memory leaks.
1 . Frequent Memory leakage. Memory leakageCodeIt will be executed multiple times, causing a memory leak each time it is executed.
2 . Occasional Memory leakage. Memory leakage occurs only in certain environments or operations. The frequency and frequency are relative.
3 . One-time memory leakage. The code with Memory leakage is executed only once, orAlgorithmAs a result, there will always be one and only one memory leak.
4 . Implicit Memory leakage. The program continuously allocates memory during the running process, but it does not release the memory until it ends. Strictly speaking, there is no memory leakage because the program releases all requested memory. However, if a server program needs to run for several days, weeks, or even months, failing to release the memory in time may eventually exhaust all the system memory. Therefore, we call this type of Memory leakage an implicit memory leak.

 

 

 

  C  ++  To obtain and release the memory. Before learning about the principle of memory leak detection, we need to understand C  ++ The working principle of new and delete in. First, we need to understand the new and delete processes.
New Operator
In general, we are writing C ++ During the program, the corresponding memory is obtained by calling a simple keyword new, so that we think it is the alias of malloc in C language. But in fact, c ++ The following three actions are required to call new:
1 ) Obtain the memory size to be applied
2 Call the corresponding form of operator new to obtain a raw memory.
3 ) If there is a constructor, the corresponding constructor is called for memory initialization.
C ++ The six types of reconstruction are as follows:
Void * Operator New (STD: size_t count Throw (STD: bad_alloc ); // Default version
Void * Operator New (STD: size_t count, Const STD: nothrow_t & ) Throw ();
Void * Operator New (STD: size_t count, Void * PTR) Throw ();
// This is the version we used for refactoring, with additional parameters
Void * Operator New [] (STD: size_t count) Throw (STD: bad_alloc );
Void * Operator New [] (STD: size_t count, Const STD: nothrow_t & ) Throw ();
Void * Operator New [] (STD: size_t count, Void * PTR) Throw ();

Operator delete also has two reconstruction forms:
Void OperatorDelete (Void *);
Void OperatorDelete [] (Void *);

 

The difference between new [], new Delete [], and delete is continuous.

Operator new [] and operator Delete [] are implemented by calling operator new and operator delete, and are exactly the same (that is to say, the actions are the same when the memory is allocated and released ).

But do not forget that the order of newoperator execution is as follows:

1. First call operator new to apply for memory.

2. Call the constructor of an object.

3. Use the constructed object to initialize the memory.

 

It can be imagined that the operator new [] process is the same as this, but the size of the allocated memory and the number of generated objects (number of constructor calls) are different.

 

The operator Delete process is equivalent to the reversal of the new operator.

1. Call the destructor of an object

2. release memory

So here we can guess the difference between Delete [] and delete: the number of calls to the Destructor (delete only calls the destructor of the first object, delete [] calls the destructor of all objects ). If the object itself does not have destructor (for example, it is only a basic type), the effects of Delete and delete [] are the same, but it is certainly not recommended because it is at least confusing.

 

The principle of Memory leakage is relatively simple:

1. First define the restructured version of operator new []; operator Delete [] OPERATOR Delete; here we manage the memory application and release, so that we can know that there is a memory leak.

2. define two macros: Define debug_new (_ file _, _ line _) (Change New to the new one we reconstructed) define new debug_new (this is the control switch, where detection is required, where is it. define this macro in the CPP file)

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.