mcpField
M
memory-safety-patterns

memory-safety-patterns

github.com/wshobsonDevelopment41.9k11.9k installsUpdated 2w ago
Repository

About memory-safety-patterns

A cross-language guide to memory-safe programming for systems developers managing resources or investigating memory bugs. It covers RAII, ownership, smart pointers, safety trade-offs, and debugging tools.

Key features

  1. 01Common memory bug categories
  2. 02RAII and resource lifetimes
  3. 03Ownership and smart pointer practices
  4. 04Language safety trade-offs
  5. 05Memory debugging tool examples

Use cases

  1. 01Writing memory-safe systems code
  2. 02Managing files, sockets, and memory
  3. 03Investigating leaks, dangling pointers, and data races

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/wshobson/agents --skill memory-safety-patterns

After installing, type/memory-safety-patternsin Claude Code to confirm it loaded.

Bundled filesreferences/details.md

Details

From the project README

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

Comments

Markdown supported