Mastering C++ Smart Pointers: unique_ptr, shared_ptr, weak_ptr. Triggers: memory management, cycles, enable_shared_from_this, make_unique, make_shared, custom deleters.
How many owners does this resource to have?
std::unique_ptr (90% of cases).std::shared_ptr.std::weak_ptr.| Issue | Design Question |
| ------------------------ | ---------------------------------------------------------------- |
| Memory Leak (Cycles) | Do you have shared_ptr pointing to each other? Cycle = Leak. |
| Dangling Pointer | Did you store a weak_ptr or raw pointer but check it too late? |
| Double Free | Did you make two unique_ptrs from one raw pointer? |
| Performance | Are you copying shared_ptr unnecessarily? (Atomic ops). |
Can I use unique_ptr?
std::move).Is this a Cycle?
shared_ptr).weak_ptr).shared_ptr to Parent, they never die.Do I need a custom deleter?
FILE*, SDL_Surface*)?unique_ptr<FILE, DeclType(&fclose)> handles this perfectly.Trace Up:
shared_ptr cycle. The reachable memory is technically "owned", just mutually.weak_ptr.Trace Down:
std::vector<std::unique_ptr<Base>>| Pattern | Cost | Use When |
| ----------------------------- | ------------- | ------------------------------------------------- |
| make_unique | Zero | Creating new heap objects. |
| make_shared | 1 Alloc | Creating access-controlled shared objects. |
| weak_ptr | Control Block | Breaking cycles, Caching. |
| enable_shared_from_this | Zero | Need shared_from_this() inside member function. |
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer