Avoid Memory Leaks

How can we use an overloaded operator to prevent memory leaks in C ++?

Any complete example.

Hello,

P

+4
source share
4 answers

If you want to avoid memory leaks, do not use delete .

This may seem paradoxical, but the truth is that manual memory management is error prone, it is better to use an automatic (or library) technique.

In C ++, there must be a clear ownership right for each object created. That is, you should be able to identify the lifetime of an object, possibly depending on some others.

The first step is to avoid allocating dynamic memory: if you are not using new , you have nothing to get around - caveat: some library will give you memory and expects you to free it. Therefore, whenever possible, use the stack .

Much use of new can be avoided by using STL containers (e.g. t23>) instead of translating your own situations.

The second step is to use new sparingly and always transfer memory to one owner immediately after allocating it. These owners include:

One owner is easy to track, and since the lifetime of an object is tied to its owner, so the lifetime of an object is also easily tracked.

The third step is the delicate, introduction of shared ownership. This really complicates all discussions about the lifetime of an object and introduces the risk of link cycles, which actually means a memory leak. They are required in some situations, but best avoided when possible.

  • std::shared_ptr (C ++ 0x) or equivalent ( std::tr1::shared_ptr , boost::shared_ptr )
  • std::weak_ptr (C ++ 0x) or equivalent

The latter is used to break cycles. However, it quickly becomes difficult to figure out where to enter weak_ptr , even with a relationship graph.

EDIT:

As Tobias noted, this idiom is known as Resource Initialization (RAII), which is awkwardly named. A new term appears: limited resource management (SBRM) to describe a subset of it β†’ resource binding to a domain.

+11
source

To add some more generality to Mattius's answer:

Whenever you use a resource that needs to be freed up (memory, network connections, file descriptors, window descriptors, ...), use Resource Initialization - RAII) .

One manifestation of this idiom is std :: unique_ptr and boost :: scoped_ptr, mentioned above. If you do not have an RAII container for the required resource, create it. It is always worth it.

+2
source

Most people recommend using Boost or STL, but there are times when this is not possible (when developing an operating system, embedded systems with limited resources, etc.). In this case, make sure you use the stack whenever possible and use only new inside the constructor of the class and delete inside its handle. There are some tools for double checking to help you find memory leaks, like valgrind .

+2
source

If you want to avoid memory leaks, do not use your own boost.shared_ptr solution. If you really want to do this manually, then put your clearing code in the destructor.

+1
source

Source: https://habr.com/ru/post/1344729/


All Articles