C ++ notes - smart pointer shared_ptr

Reference:
https://zh.cppreference.com/w/cpp/memory/shared_ptr
https://blog.csdn.net/caoshangpa/article/details/79392878
https://blog.csdn.net/yuhan61659/article / details / 81563629

shared_ptr smart pointer

The purpose : to solve the problem with dynamically allocated memory space for new pieces in the execution path of the program can be released, to avoid memory leaks
C ++ 11 Template Library Smart pointers defined in the header file, that is shared _ptr template problem can be solved.

The new operator returns a pointer to a shared_ptr objects p "managed", you do not have to worry about where to write delete p statement, no write delete statement, p hosted the shared_ptr object is automatically executed when the delete p demise. Its usage is similar to the pointer.
Smart pointer is 模板类not a pointer. Similar vector, is also a smart pointer template when creating a smart pointer, you must provide additional information that is 指针可以指向的类型. The default initialization smart pointer in a preserved 空指针. The use of smart pointers similar to an ordinary pointer. Dereference a smart pointer return the object to which it points. If smart pointers in a determination condition, is to detect the effect of it 是否为空.

Smart pointer is a pointer to an object dynamically allocated (heap) for the lifetime of control, to ensure that the correct object is automatically destroyed dynamically allocated to prevent memory leaks. It is a common technique is to implement reference counting. Each time it is used, the internal reference count is incremented each time a destructor, internal reference count by one, it decreases to 0, deleted pointed heap memory.
Each copy of shared_ptr point to the same memory. When the last shared_ptr destructor, the memory will not be released.

Can 构造函数, 赋值函数or make_shared函数initialize the smart pointer.


Supported methods
(from C ++ Primer Fifth Edition Chinese version):
Supported methods


Use shared_ptr Notes:
(1) Do not put a 原生指针for more shared_ptr management;
(2), not to this指针give shared_ptr; (Note 1, if they want to use this situation pointers, how to solve? enable_shared_from_this)
(3), do not Creating the function arguments in shared_ptr;
(4), do not uncritically replace pointer shared_ptr to prevent memory leaks, shared_ptr is not a panacea, but if they are used also requires a certain overhead;
(5), ring the chain structure shared_ptr will cause a memory leak (can be combined to solve weak_ptr);
(6), the general shared ownership to survive longer than the target scoped, which will lead to higher average resource usage time;
( 7), the 多线程use of shared pointers environment price is very large, this is because you need to avoid data races on reference counting;
(8), the shared object's destructor is 不会performed at the expected time;
(9), not using the same built-in pointer value is initialized (or reset) a plurality of smart pointer;
(10), do not delete get (returned pointer);
(11), without using get () to initialize or reset another smart pointer;
(12), If using get () returns a pointer to remember when the last corresponding smart pointer after the destruction, your pointer becomes invalid;
(13), if you are not using the new resource allocation pointer intelligent management of memory, remember it is passed to a delete.

enable_shared_from_this

Reference: https://blog.csdn.net/caoshangpa/article/details/79392878
when the class A is share_ptr management, and the need to put the current class object as a parameter passed to other functions in the class member function A's: use the occasion , you need to pass a pointer to itself share_ptr.
enable_shared_from_this is a template class, defined in the header file Its prototype is:

template< class T > class enable_shared_from_this;

std :: enable_shared_from_this make an object (assuming it called t, pt management and has been a std :: shared_ptr objects) securely generate other additional std :: shared_ptr instance (assuming that the name pt1, pt2, ...) they share ownership of the object t and pt.
If a class T inherits std :: enable_shared_from_this It will provide for the T class member function: shared_from_this. When T t is a type of object is named pt std :: shared_ptr When the class object management, call T :: shared_from_this member function will return a new std :: shared_ptr Objects that share ownership t and pt.

1. Why not just pass this pointer:
       use smart pointers mind is to facilitate resource management, if you use smart pointers in some places, some places use raw pointers, it is easy to damage the semantic smart pointer, resulting in a variety of errors.
2. You can directly transfer share_ptr Why?
       The answer is no, because this will cause two unshared share_ptr the same object, is not increased twice Count destructor object reference guide.

Guess you like

Origin www.cnblogs.com/gnivor/p/12554068.html