std::shared_ptr<T>::~shared_ptr
From cppreference.com
< cpp | memory | shared ptr
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Memory management library
(exposition only*)
(C++11)
(C++23)
(C++11)
(C++17)
(C++11)
(C++11)
(C++20)
(C++20)
(C++17)
(C++11)
(C++17)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
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)
(C++11)
(C++11)
(C++17)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
(until C++17*)
(C++11)
(C++17)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++11)
(C++20)
(C++11)
(C++11)
(C++20)
(C++26)
std::shared_ptr
Member functions
shared_ptr::~shared_ptr
Modifiers
Observers
(C++17)
(until C++20*)
(C++26)
(C++26)
Non-member functions
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
functions (until C++26*)
Helper classes
(C++20)
Deduction guides (C++17)
~shared_ptr();
If *this owns an object and it is the last shared_ptr
owning it, the object is destroyed through the owned deleter.
After the destruction, the smart pointers that shared ownership with *this, if any, will report a use_count() that is one less than its previous value.
[edit] Notes
Unlike std::unique_ptr , the deleter of std::shared_ptr is invoked even if the managed pointer is null.
[edit] Example
Run this code
#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" }
Output:
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()