smithery.ai

m02-resource

Mastering C++ Smart Pointers: unique_ptr, shared_ptr, weak_ptr. Triggers: memory management, cycles, enable_shared_from_this, make_unique, make_shared, custom deleters.

First seen Apr 8, 2026

Installation

$ npx skills add https://smithery.ai

Similar popular skills

Related neighbors and high-traction skills in the same topics — useful to compare before installing.

Also in this package

Other skills from smithery.ai · top by installs.

npx skills add https://smithery.ai

Browse all from smithery.ai

More details

Agent compatibility

Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.

Claude Code Not declared
Cursor Not declared
Codex Not declared
GitHub Copilot Not declared
Windsurf Not declared
Gemini CLI Not declared
Cline Not declared
OpenCode Not declared

Package contents

Files included with this skill beyond the listing page.

  • skill md SKILL.md 2,592 B
  • docs SUMMARY.md 188 B

History

  1. First seen on skills.sh
  2. First recorded snapshot · 1 installs

SKILL.md

C++ Smart Pointers (Resource Management)

Core Question

How many owners does this resource to have?

  • One: std::unique_ptr (90% of cases).
  • Many: std::shared_ptr.
  • Observer: std::weak_ptr.

Error → Design Question

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).

Thinking Prompt

  1. Can I use unique_ptr?

- Always start here. It has zero overhead (size = raw pointer). - It forces you to think about ownership transfer (std::move).

  1. Is this a Cycle?

- Parent -> Child (sharedptr). - Child -> Parent (weakptr). - If Child holds shared_ptr to Parent, they never die.

  1. Do I need a custom deleter?

- Managing C-APIs (FILE, SDLSurface)? - uniqueptr<FILE, DeclType(&fclose)> handles this perfectly.

Trace Up / Down

  • Trace Up:

- Issue: "Application memory usage grows endlessly but no 'leaks' reported by Valgrind." - Cause: sharedptr cycle. The reachable memory is technically "owned", just mutually. - Fix: Break the cycle with weakptr.

  • Trace Down:

- Intent: "I need a list of heterogeneous objects." - Code: std::vector<std::unique_ptr<Base>>

Quick Reference

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.
enablesharedfrom_this Zero Need sharedfromthis() inside member function.