m02-resource

Manage C++ heap ownership with smart pointers and custom deleters.

14|3|Updated Jan 25, 2026
One-click install
npx skills add https://github.com/13eholder/Modern-Cpp-Skills --skill m02-resource-13eholder
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: m02-resource
Source: https://github.com/13eholder/Modern-Cpp-Skills/tree/main/m02-resource
Command: npx skills add https://github.com/13eholder/Modern-Cpp-Skills --skill m02-resource-13eholder

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps prevent memory leaks, double frees, and ownership confusion in C++ by guiding the correct selection and usage of smart pointers and custom deleters across common code patterns.

Core Features & Use Cases

  • Ownership guidance: Choose between sole ownership with std::unique_ptr, shared ownership with std::shared_ptr, and observer semantics with std::weak_ptr.
  • Cycle detection and resolution: Identify shared_ptr cycles and recommend breaking them with weak_ptr or redesigning ownership.
  • Interoperability and cleanup: Recommend make_unique and make_shared for safe allocation and advise on custom deleters for C APIs and resource wrappers.
  • Practical examples: Patterns include heterogeneous collections using vector of std::unique_ptr<Base>, parent-child graphs with parent as weak_ptr, and using enable_shared_from_this when objects need shared_from_this.

Quick Start

Ask the skill to recommend the appropriate smart pointer for each class relationship, detect potential cycles, and suggest fixes including custom deleter designs when needed.

Frequently Asked Questions about m02-resource

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
When should I use std::weak_ptr instead of std::shared_ptr in C++?

Use std::weak_ptr instead of std::shared_ptr to break ownership cycles in parent-child object graphs and provide non-owning observer references. It prevents memory leaks by allowing observation without extending the object's lifetime.

How do I wrap a C API resource handle with smart pointers in C++?

Wrap C API resource handles by designing custom deleters for std::unique_ptr or std::shared_ptr. Custom deleters ensure proper cleanup of native resources and prevent double frees when managing non-standard heap allocations.

What is the best way to choose between std::unique_ptr and std::shared_ptr for class ownership?

Choose std::unique_ptr for sole ownership where a single object controls the resource lifecycle, and std::shared_ptr for shared ownership when multiple objects need to maintain the resource lifetime simultaneously.

Why do I need enable_shared_from_this when managing C++ heap resources?

You need std::enable_shared_from_this when an object must create additional std::shared_ptr instances pointing to itself during its own member methods. This prevents double frees by safely sharing ownership without duplicating control blocks.

Can I use smart pointers for heterogeneous collections in C++?

Yes, you can build heterogeneous collections using a std::vector of std::unique_ptr<Base>. This pattern manages heap resources for derived class objects polymorphically while ensuring automatic memory leak prevention.