0Pricing
C++ Academy · Lesson

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 — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 new in 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.

Frequently asked questions

Is the “Custom Deleters and Make Functions” lesson free?

Yes — the full text of “Custom Deleters and Make Functions” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Why Smart Pointers Replace Raw new delete
  2. unique_ptr Exclusive Ownership
  3. shared_ptr and weak_ptr Shared Ownership
  4. Custom Deleters and Make Functions
← Back to C++ Academy