C++ Reference ยท Memory and Utilities
Custom deleters for smart pointers in C++
How to give unique_ptr and shared_ptr a custom deleter: function pointers, functors and lambdas, FILE* handles, the size cost and the differences between both.
A deleter is the callable a smart pointer runs when it is time to release the resource. By default unique_ptr<T> uses std::default_delete<T> (which calls delete), and shared_ptr remembers how the pointer was created. With a custom deleter the same smart pointers can manage anything that needs a cleanup call: a FILE*, a socket, a C library handle, memory from malloc.
The two smart pointers treat deleters differently. For unique_ptr the deleter is part of the type, so there is no runtime cost. For shared_ptr it is type-erased in the control block, so the pointer type stays the same.
std::unique_ptr<FILE, int(*)(FILE*)> f(std::fopen("a.txt", "r"), &std::fclose); std::shared_ptr<int> s(new int[3], [](int* p) { delete[] p; });
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template<class T, class Deleter = std::default_delete<T>> class unique_ptr; unique_ptr(pointer p, Deleter d); // unique_ptr: deleter in the type shared_ptr(Y* p, Deleter d); // shared_ptr: deleter erased shared_ptr(Y* p, Deleter d, Allocator a);
| Name | Description |
|---|---|
Deleter |
A callable taking the stored pointer. It is called once, with a non-null pointer for unique_ptr. |
get_deleter() |
Returns a reference to the stored deleter of a unique_ptr. std::get_deleter<D>(sp) does the same for a shared_ptr. |
Errors: the deleter must not throw. If constructing the shared_ptr fails with bad_alloc, the deleter is called on the pointer immediately.
Usage notes
- A
unique_ptrwith an empty (stateless) deleter, such as a captureless lambda or a functor, is the same size as a raw pointer. A function-pointer deleter adds one pointer of storage. unique_ptrnever calls the deleter on a null pointer.shared_ptrcalls it even if you handed itnullptr, so make your deleter tolerate null where that can occur.- Prefer a small functor or captureless lambda alias (
using FilePtr = std::unique_ptr<FILE, decltype(&std::fclose)>;) over repeating the type. - Use a deleter for memory from
mallocor a C API (free,SDL_DestroyWindow,sqlite3_close). Using the default deleter on such memory is undefined behavior. - A
shared_ptrdeleter is invisible in the type, so two pointers with different deleters can live in the same container. This flexibility costs one indirect call and heap storage in the control block. std::make_uniqueandstd::make_sharedcannot take a deleter; construct directly.- Keep the deleter's own destruction order in mind: members are destroyed in reverse order, so a deleter that uses another object needs that object to outlive the pointer.
- For wrapping something that is not a pointer (such as an integer file descriptor), write a small RAII class instead; see RAII.