The main steps are twofold:
First, keep in mind that for each new deletion is required. So, when you use the new operator, realize what this object will do, how it will be used and how its life will be controlled.
Secondly, make sure you never overwrite the pointer. You can do this using a class of smart pointers instead of raw pointers, but if you really make sure you never use it with an implicit conversion. (example: using the MSXML library, I created the CCOMPtr smart pointer to store the nodes to get the node, you call the get_Node method, passing to the address of the smart pointer that had a conversion operator that returned the pointer-pointer type Unfortunately, this meant that if the smart pointer already stores data, the data of this element will be overwritten, the previous node will leak).
I think these two cases are a time when you can skip memory. If you use only the smart pointer directly - never allowing you to reveal your internal data, you can be safe from the latest problem. If you wrap all your code that uses the new one and delete it in the class (i.e. using RAII), you too can be safe from the first.
Avoiding memory leaks in C ++ is very simple if you follow the above steps.
gbjbaanb
source share