Custom Deleters and Make Functions
Use std::make_unique, std::make_shared, and custom deleters for non-memory resources.
Custom Deleters and Make Functions is a free C++ Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Why Custom Deleters?
Not every resource is freed by delete. File handles, sockets, OS-specific resources, and arrays each need a different cleanup function.
Default Deleter for unique_ptr
By default unique_ptr<T> uses delete. For arrays, use unique_ptr<T[]> which calls delete[].
Specifying a Custom Deleter
Pass a callable as the second template argument. The deleter receives the raw pointer and frees it appropriately.
auto closer = [](FILE* f) { if (f) std::fclose(f); };
std::unique_ptr<FILE, decltype(closer)> file(
std::fopen("data.txt", "r"), closer);Deleter for shared_ptr
shared_ptr stores its deleter in the control block — the type is type-erased, so the shared_ptr type itself does not change.
std::shared_ptr<FILE> file(
std::fopen("data.txt", "r"),
[](FILE* f) { if (f) std::fclose(f); }
);Common Custom Deleter Use Cases
Real-world examples:
- C library handles (FILE*, sqlite3*, curl handles)
- OS resources (HANDLE on Windows, fd on Unix)
- Custom memory pools
- Reference-counted external objects
std::make_unique (C++14)
The recommended way to create a unique_ptr. Forwards arguments to the constructor and avoids raw new.
auto p = std::make_unique<Widget>(arg1, arg2);Why make_unique?
Three benefits:
- Exception safety — no raw
newin user code - No need to type the type twice
- Cannot accidentally pass the raw pointer to two unique_ptrs
std::make_shared
For shared_ptr, make_shared allocates the control block and the object in one heap allocation — faster than separate allocations.
auto p = std::make_shared<Widget>(arg1, arg2);When make_shared Has Drawbacks
The single allocation means the object s memory is only freed when all weak_ptrs are also gone. If your weak_ptrs outlive the shared_ptrs, prefer the two-step construction.
Combining Custom Deleters with make
make_unique and make_shared do not directly accept a custom deleter. Construct the smart pointer manually in that case.
// make_shared cannot take a deleter
std::shared_ptr<FILE> file(
std::fopen("data.txt", "r"),
[](FILE* f) { if (f) std::fclose(f); }
);Pitfall: Lambda Type as Template Parameter
Each lambda has a unique type. For unique_ptr, use decltype(my_deleter) when declaring; or use a named function or function pointer to keep types simple.
Quick Check
Which factory function lets you allocate a std::shared_ptr and its control block in one heap allocation?
Recap
Use make_unique and make_shared for routine cases. Provide a custom deleter (a lambda or function) when the resource needs more than delete — file handles, OS objects, or C library cleanup.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Custom Deleters and Make Functions” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Custom Deleters and Make Functions”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Custom Deleters and Make Functions”?
Use std::make_unique, std::make_shared, and custom deleters for non-memory resources. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Custom Deleters and Make Functions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Smart Pointers Replace Raw new delete
- unique_ptr Exclusive Ownership
- shared_ptr and weak_ptr Shared Ownership
- Custom Deleters and Make Functions