Manual ownership of an object versus smart pointers

Right now, the ownership / deletion of an object in my C ++ project is tracked manually (most often in the comments). Almost every object selected by the heap is created using factory views.

eg.

auto b = a->createInstanceOfB(); //a owns b auto c = b->createInstanceOfC(); //b owns c //auto k = new K(); //not in the code ... //b is no longer used.. a->destroyInstanceOfB(b); //destroyInstanceOf calls delete on it 

What benefits, if any, will smart pointers contain in this setting?

+4
source share
3 answers

This is not a creature that you should worry about, it is a deletion.

With smart pointers (type of reference counting), objects can usually be several other objects, and when the last link goes out of scope, the object is automatically deleted. That way, you no longer have to manually delete anything, you can only leak memory when you have circular dependencies and your objects are never deleted from behind.

The type for only one owner ( std::auto_ptr ) also frees you from your legal capacity, but it allows only one owner at a time (although ownership can be transferred). This is useful for objects that you pass as pointers, but you still want them to be automatically cleaned when they go out of scope (so that they work well in containers, and stack reversal in case of exception works as expected).

In any case, smart pointers make explicit ownership of your code not only for you and your teammates, but also for the compiler - improper execution will likely result in a compiler error or a runtime error that is relatively simple to catch the protective encoding. In the memory-driven manual code, it is easy to get the ownership situation somewhere wrong (due to incorrect reading of comments or assuming something is in the wrong way), and the resulting error is usually difficult to track - you will be a memory leak, overwriting what is not yours, the program accidentally crashes, etc .; they all have in common that the situation in which the error occurs is not related to the violation code section.

+9
source

Smart pointers provide semantics of ownership, that is, they guarantee that the object will be released correctly even in the event of exceptions. You should always use them solely for security reasons, even if they express only very simple semantics such as std :: unique_ptr. Moreover, a pointer that applies semantics reduces the need to document it, and less documentation means the documentation is outdated or incorrect, especially if several parts of the same program express the same semantics.

Ultimately, smart pointers reduce the number of error sources and there is little reason not to use them.

+2
source

If an object belongs to only one other object and dies with it, fine. You still need to make sure that there are no dangling links, but this is not a bad case.

A difficult case is where you share your property. In this case, you want smart-ptrs (or something else) to automatically detect when to actually delete the object.

Please note that co-ownership is not necessary everywhere, and avoiding it is likely to simplify the situation when your product becomes bloated. :)

+1
source

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


All Articles