In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the common memory leaks in C++". The content in the article is simple and clear, and it is easy to learn and understand. Now please follow the editor's train of thought slowly and deeply. Let's study and learn what are the common memory leaks in C++!
1. C++ or C # does not call a matching malloc free new delete to cause memory leakage, which is the root cause. Constructor new remember destructor delete
two。 Sometimes the application is int * p = new int [10]; delete p on release; the correct one should be delete [] p; or there is a nested new where we only delete the outermost
3. Sometimes the system crashes and the destructor cannot be called, resulting in a memory leak and the introduction of auto_ptr smart pointers.
4. Sometimes we use the copy constructor in new memory, because the default copy constructor is a shallow copy, and during destructing, there is a repeated release of the same resource, delete error. Note the deep copy.
5. Failure to write the base class destructor to type virtual can also cause memory leaks
Wild pointer
1. No value assigned for pointer initialization
two。 There is no empty NULL after the pointer delete to make the pointer point to junk memory.
3. Pointer operation exceeds its own permission to exceed the authority segment error
1. There are no matching calls to new and delete functions in the constructor and destructor of the class
This kind of memory leak occurs in two cases: first, objects are created in the heap to occupy memory, but the memory occupied by objects is not explicitly freed; second, memory is dynamically allocated in the constructor of the class. but memory is not freed in the destructor or not properly freed
two。 Nested object pointers are not cleared correctly
3. Square brackets are not used in delete when releasing an array of objects
Square brackets tell the compiler that the pointer points to an array of objects, and it also tells the compiler that the correct object address value calls the destructor of the object. If there are no square brackets, then the pointer points to only one object by default. The destructors of other objects in the object array will not be called, resulting in a memory leak. If you put a number larger than the size of the object array in the middle of the square brackets, the compiler will call 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 call enough destructors, resulting in a memory leak.
Releasing a single object, a variable of a single basic data type, or an array of basic data types does not require a size parameter. Releasing an array of objects that define a destructor requires a size parameter.
4. An array of pointers to an object is not the same as an array of objects
Object array means that objects are stored in the array, and only delete [] p is needed to free up space by calling the destructor of each object in the object array.
The pointer array to the object refers to: the pointer to the object is stored in the array, which releases not only the space of each object, but also the space of each pointer. Delete [] p only releases each pointer, but does not release the space of the object. The right thing to do is to release each object through a loop, and then release the pointer.
5. Missing copy constructor
Releasing the same memory twice is a mistake and may cause the heap to collapse.
Passing by value invokes (copies) the constructor, and reference passing does not.
In C++, if no copy constructor is defined, the compiler calls the default copy constructor and copies the data members on a member-by-member basis. If copying pointers on a member-by-member basis is defined to assign the address of one variable to another. 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 released, its destructor frees up dynamically allocated memory space associated with that object. When the second object is released, its destructor frees the same memory, which is wrong.
So, if a class has pointer member variables, either the write copy constructor and overload assignment operator must be displayed, or the copy constructor and overload assignment operator must be disabled
6. Missing overloaded assignment operator
This problem is similar to the above problem, in which objects are copied member by member. If the size of this class is variable, the result is a memory leak, as shown below:
7. Common myths about nonmodifying operator overloading
a. Returns a reference or pointer to an object on the stack (that is, a reference or pointer to a local object). Causes a null reference or null pointer to be returned at last, so it becomes a wild pointer
b. Returns a reference to an internal static object.
c. Returns an object that leaks a dynamically allocated memory. Causes a memory leak and cannot be recycled
The solution to this kind of problem is that the return value of the overloaded operator function is not a reference to the type, and the second is the return value of the type, that is, not int& but int.
8. The base class destructor is not defined as a virtual function
When the base class pointer points to the subclass object, if the destructor of the base class is not virtual, then the destructor of the subclass will not be called and the resources of the subclass will not be released correctly, resulting in a memory leak
Wild pointer: a pointer to freed or access restricted memory.
The cause of the wild pointer:
The pointer variable is not initialized (if the value is uncertain, it can be initialized to NULL)
After the pointer is free or delete, it is not set to NULL. Free and delete just release the memory pointed to by the pointer and do not kill the pointer itself. In this case, the pointer points to "junk" memory. The released pointer should be set to NULL.
Pointer operations go beyond the scope of variables. For example, pointers that return to stack memory are wild pointers.
Thank you for your reading, the above is the content of "what are the common memory leaks in C++". After the study of this article, I believe you have a deeper understanding of the common memory leaks in C++. Specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
Package com.zyhao.openec.util;import java.io.File;import java.io.FileInputStream;import java.io.File
© 2024 shulou.com SLNews company. All rights reserved.