Namespaces
Variants

std::shared_ptr<T>:: ~shared_ptr

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
~shared_ptr ( ) ;

만약 * this 가 객체를 소유하고 그것이 해당 객체를 소유하는 마지막 shared_ptr 인 경우, 객체는 소유된 deleter를 통해 파괴됩니다.

파괴 후에, * this 와 소유권을 공유하던 스마트 포인터들이 있다면, use_count() 는 이전 값보다 하나 적은 값을 보고할 것입니다.

참고 사항

std::unique_ptr 와 달리, std::shared_ptr 의 deleter는 관리되는 포인터가 null인 경우에도 호출됩니다.

예제

#include <iostream>
#include <memory>
struct S
{
    S() { std::cout << "S::S()\n"; }
    ~S() { std::cout << "S::~S()\n"; }
    struct Deleter
    {
        void operator()(S* s) const
        {
            std::cout << "S::Deleter()\n";
            delete s;
        }
    };
};
int main()
{
    auto sp = std::shared_ptr<S>{new S, S::Deleter{}};
    auto use_count = [&sp](char c)
    {
        std::cout << c << ") use_count(): " << sp.use_count() << '\n';
    };
    use_count('A');
    {
        auto sp2 = sp;
        use_count('B');
        {
            auto sp3 = sp;
            use_count('C');
        }
        use_count('D');
    }
    use_count('E');
//  sp.reset();
//  use_count('F'); // would print "F) use_count(): 0"
}

출력:

S::S()
A) use_count(): 1
B) use_count(): 2
C) use_count(): 3
D) use_count(): 2
E) use_count(): 1
S::Deleter()
S::~S()

참고 항목

weak_ptr 를 파괴합니다
( std::weak_ptr<T> 의 public 멤버 함수)