Generated banner for Memory Safety Patterns
Generated logo for Memory Safety Patterns

Memory Safety Patterns

Seth HobsonCommunityCreated by Seth Hobson

  • Skill
  • Guidelines
  • Software development
  • MIT
  • No issues found

From the author

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.
— the description by Seth Hobson

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill provides a structured approach to memory safety by detailing cross-language patterns such as RAII, ownership, and smart pointers. It helps developers write robust systems code by explaining how to prevent common memory bugs like use-after-free, leaks, and data races. The content covers core concepts, best practices, and debugging strategies for C, C++, and Rust, offering a clear path to safer resource management.

What it does

  • Defines core memory safety patterns like RAII and ownership.
  • Categorizes common memory bugs and their prevention strategies.
  • Provides C++ examples for smart pointers and RAII.
  • Explains Rust ownership and borrowing concepts.
  • Lists essential debugging tools for memory and thread safety.
  • Offers best practices for avoiding raw pointers and dangling references.

When to use it

  • When writing or refactoring systems-level code.
  • When you need to manage complex resources like files or sockets.
  • When debugging memory leaks or use-after-free errors.
  • When choosing between languages based on their safety guarantees.
  • When implementing RAII to ensure deterministic resource cleanup.

Advantages

  • Reduces the risk of undefined behavior in systems programming.
  • Provides clear, actionable patterns for multiple languages.
  • Encourages modern, exception-safe coding practices.
  • Helps bridge the gap between manual memory management and safe abstractions.

Usage examples

“How can I prevent double-free errors in C++?”
The skill explains using smart pointers and RAII to ensure resources are managed automatically, preventing manual free calls that lead to double-free bugs.
“What is the best way to handle file handles safely?”
It demonstrates the RAII pattern, where a class constructor opens the file and the destructor ensures it is closed, tying resource lifetime to object scope.
“How do I debug memory leaks in my C++ application?”
The skill provides commands for using Valgrind and AddressSanitizer to detect leaks and invalid memory access during runtime.

Install

  1. Download the skill as a ZIP:

    https://mp-staging.autogineer.com/agent-skills/wshobson/memory-safety-patterns/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip memory-safety-patterns.zip -d .agents/skills

Documentation by Seth Hobson

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

Files

SKILL.md

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

Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Created by
Seth Hobson
Publisher
Seth Hobson (Community)
Source
wshobson/agents · plugins/systems-programming/skills/memory-safety-patterns · commit 4bde316
Licence
MIT License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE)
  • ATTRIBUTION.md (written by AutoGineer)
Synced

AutoGineer is not affiliated with or endorsed by Seth Hobson.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download memory-safety-patterns.zip