Several cases of C + + memory leaks

Source: Internet
Author: User

1. There is no matching call to the new and delete functions in the constructor and destructor of the class

In both cases, this memory leak occurs: the object that is created in the heap consumes memory, but does not display the memory used by the object, and the memory is allocated dynamically in the constructor of the class, but no memory is freed in the destructor or the memory is not freed properly

2. Nested object pointers are not cleared correctly

3. No square brackets are used in delete when releasing an array of objects

The square brackets tell the compiler that this pointer is to an array of objects, and also tells the compiler that the correct object address value is the destructor of the calling object, and if there is no square bracket, the pointer is defaulted to only one object, and the destructor of other objects in the object array is not called, resulting in a memory leak. If you put a number in the middle of the square bracket that is larger than the size of the object array, then the compiler invokes the destructor of the invalid object (memory overflow), causing the heap to collapse. If the numeric value in the middle of the square brackets is smaller than the size of the object array, the compiler cannot invoke enough destructors, resulting in a memory leak.

Releasing a single object, a variable of a single base data type, or an array of basic data types does not require a size parameter, and an array of objects that define the destructor is required for the size parameter.

4. An array of pointers to objects is not equivalent to an array of objects

An array of objects means that the object is stored in the array, and only the delete []p]can be called to free space from the destructor of each object in the object array.

An array of pointers to objects means that the array holds pointers to objects, not only to free space for each object, but also to free space for each pointer, anddelete []p only releases each pointer, but does not release the object's space, the correct way is through a loop, Each object is freed, and then the pointer is released.

5. Missing copy constructor

Two times releasing the same memory is a bad practice, and can cause a heap collapse.

Passing by value invokes (copies) the constructor, and reference delivery is not called.

In C + + , if the copy constructor is not defined, the compiler invokes the default copy constructor, copying the data members in a copy-by-member manner, and copying the pointer in a copy-by-member manner is defined as assigning the address of one variable to another variable. The result of this implicit pointer copy is that two objects have pointers to the same dynamically allocated memory space. When the first object is disposed, its destructor frees the dynamically allocated memory space associated with the object. When you release the second object, its destructor releases the same memory, which is wrong.

So, if there is a pointer member variable inside a class, either the write-copy constructor and the overloaded assignment operator must be displayed, or the copy constructor and the overloaded assignment operator are disabled

6. Missing overloaded assignment operator

This problem is similar to the above problem, but also copy the object in a copy-by-member way, if the size of the class is variable, then the result is a memory leak, such as:

7. Common myths about nonmodifying operator overloading

A. Returns a reference or pointer to an object on the stack (which also returns a reference or pointer to a local object). The result is a null reference or null pointer, so it becomes a wild pointer

B. Returns a reference to an internal static object.

C. Returns a dynamically allocated object that leaks memory. Causes a memory leak and cannot be recycled

The solution to this type of problem is that the return value of the overloaded operator function is not a reference to the type, and the second should be the return value of the type, which is not int& but int

8. The base class's destructor is not defined as a virtual function

When the base class pointer points to a subclass object, if the destructor of the base class is not virtual, then the destructor of the subclasswill not be called, and the resource of the subclass is not properly disposed, thus causing a memory leak

Wild pointer : A pointer to the released or access limited memory.

Causes of wild pointers:

    1. Pointer variable is not initialized (can be initialized to NULL if the value is indeterminate )
    2. When the pointer is free or deleted, it is not set to NULL, and both the zero and the delete simply release the memory pointed to by the pointer and do not kill the pointer itself, pointing to the "garbage Memory The released pointer should be set to NULL.
    3. Pointer manipulation goes beyond the scope of a variable, such as a pointer to a stack of memory is a wild pointer.

Several cases of C + + memory leaks

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.