PluginBench
Skill
Pass
Audit score 90

memory-safety-patterns

wshobson/agents

Cross-language RAII, ownership, and smart pointer patterns for memory-safe Rust, C++, and C code.

What is memory-safety-patterns?

This skill provides cross-language reference patterns for memory-safe programming, covering RAII, ownership, and smart pointers in Rust, C++, and C. Use it when writing systems code, managing resources like files/sockets/memory, or debugging memory bugs such as use-after-free and leaks.

  • Documents categories of memory bugs and their prevention techniques
  • Outlines a safety spectrum comparing manual memory management, smart pointers, ownership, and garbage collection
  • Provides Do's and Don'ts for memory-safe coding practices
  • Lists commands for memory debugging tools: AddressSanitizer, Valgrind, Rust Miri, ThreadSanitizer
  • Points to a references/details.md file with detailed patterns and worked examples

How to install memory-safety-patterns

npx skills add https://github.com/wshobson/agents --skill memory-safety-patterns
Prerequisites
  • A coding agent environment with skill installation support (e.g., Claude Code, Cursor)
  • Toolchain for the target language(s): Clang/GCC for C/C++, Rust nightly toolchain for Miri
  • Optional: Valgrind installed for leak checking
Claude Code
Cursor
Windsurf
Cline

How to use memory-safety-patterns

  1. 1.Invoke the skill when writing or reviewing systems code in Rust, C++, or C that involves resource or memory management
  2. 2.Reference the memory bug category table to identify which bug class you're trying to prevent (use-after-free, double-free, leak, overflow, dangling pointer, data race)
  3. 3.Apply the recommended pattern: RAII for resource lifetimes, smart pointers in C++, ownership/borrowing in Rust
  4. 4.Follow the Do's and Don'ts list when writing or refactoring code
  5. 5.Consult references/details.md for detailed patterns and worked examples when the top-level guidance isn't enough
  6. 6.Run the relevant debugging tool (AddressSanitizer, Valgrind, Rust Miri, or ThreadSanitizer) using the provided commands to verify memory safety

Use cases

Good for
  • Writing new systems code that needs safe resource management (files, sockets, memory)
  • Refactoring C/C++ code to use RAII and smart pointers instead of raw pointers
  • Choosing which language's safety model fits a project's risk/control tradeoff
  • Debugging use-after-free, double-free, or leak issues using AddressSanitizer, Valgrind, or Miri
  • Reviewing code for unsafe patterns like returning local references or careless use of Rust's unsafe blocks
Who it's for
  • Systems programmers working in Rust, C++, or C
  • Developers debugging memory corruption or leak issues
  • Engineers deciding between languages/approaches for memory safety
  • Code reviewers checking for unsafe pointer usage or resource management bugs

memory-safety-patterns FAQ

Which languages does this skill cover?

Rust, C++, and C, with comparisons across their safety models (manual memory in C, smart pointers in C++, ownership in Rust).

Does this skill fix memory bugs automatically?

No. It provides patterns, guidance, and tool commands (AddressSanitizer, Valgrind, Miri, ThreadSanitizer) to help you write safe code and detect issues yourself.

What kinds of memory bugs does it address?

Use-after-free, double-free, memory leaks, buffer overflows, dangling pointers, and data races, along with prevention techniques for each.

Is there more detail beyond the SKILL.md file?

Yes, detailed pattern documentation and worked examples are in a references/details.md file that the agent reads when needed.

Full instructions (SKILL.md)

Source of truth, from wshobson/agents.


name: memory-safety-patterns description: Implement memory-safe programming with RAII, ownership, smart pointers, and resource management across Rust, C++, and C. Use when writing safe systems code, managing resources, or preventing memory bugs.

Memory Safety Patterns

Cross-language patterns for memory-safe programming including RAII, ownership, smart pointers, and resource management.

When to Use This Skill

  • Writing memory-safe systems code
  • Managing resources (files, sockets, memory)
  • Preventing use-after-free and leaks
  • Implementing RAII patterns
  • Choosing between languages for safety
  • Debugging memory issues

Core Concepts

1. Memory Bug Categories

Bug TypeDescriptionPrevention
Use-after-freeAccess freed memoryOwnership, RAII
Double-freeFree same memory twiceSmart pointers
Memory leakNever free memoryRAII, GC
Buffer overflowWrite past buffer endBounds checking
Dangling pointerPointer to freed memoryLifetime tracking
Data raceConcurrent unsynchronized accessOwnership, Sync

2. Safety Spectrum

Manual (C) → Smart Pointers (C++) → Ownership (Rust) → GC (Go, Java)
Less safe                                              More safe
More control                                           Less control

Detailed patterns and worked examples

Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.

Best Practices

Do's

  • Prefer RAII - Tie resource lifetime to scope
  • Use smart pointers - Avoid raw pointers in C++
  • Understand ownership - Know who owns what
  • Check bounds - Use safe access methods
  • Use tools - AddressSanitizer, Valgrind, Miri

Don'ts

  • Don't use raw pointers - Unless interfacing with C
  • Don't return local references - Dangling pointer
  • Don't ignore compiler warnings - They catch bugs
  • Don't use unsafe carelessly - In Rust, minimize it
  • Don't assume thread safety - Be explicit

Debugging Tools

# AddressSanitizer (Clang/GCC)
clang++ -fsanitize=address -g source.cpp

# Valgrind
valgrind --leak-check=full ./program

# Rust Miri (undefined behavior detector)
cargo +nightly miri run

# ThreadSanitizer
clang++ -fsanitize=thread -g source.cpp