HAND-TAGGED >>> 991 SKILLS LIVE <<<* OPEN SOURCE *NO LOGIN, NO TRACKING FRESH DROPS WEEKLY HAND-TAGGED >>> 991 SKILLS LIVE <<<* OPEN SOURCE *NO LOGIN, NO TRACKING FRESH DROPS WEEKLY HAND-TAGGED >>> 991 SKILLS LIVE <<<* OPEN SOURCE *NO LOGIN, NO TRACKING FRESH DROPS WEEKLY HAND-TAGGED >>> 991 SKILLS LIVE <<<* OPEN SOURCE *NO LOGIN, NO TRACKING FRESH DROPS WEEKLY HAND-TAGGED >>> 991 SKILLS LIVE <<<* OPEN SOURCE *NO LOGIN, NO TRACKING FRESH DROPS WEEKLY HAND-TAGGED >>> 991 SKILLS LIVE <<<* OPEN SOURCE *NO LOGIN, NO TRACKING FRESH DROPS WEEKLY
← back to homepage
memory-safety-patternsSKILL #ERNS
Coding

memory-safety-patterns

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.

↗ github · ★ 37k·src: wshobson/agents

the manual

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

more coding

Request code reviews to catch issues early
Coding
HOT
Request code reviews to catch issues early
requesting-code-review
2@ 2 240k
Execute plans flawlessly and efficiently
Coding
HOT
Execute plans flawlessly and efficiently
executing-plans
0@ 0 240k
Finish your dev branch like a pro
Coding
HOT
Finish your dev branch like a pro
finishing-a-development-branch
0@ 0 240k
Verify feedback before you implement changes
Coding
HOT
Verify feedback before you implement changes
receiving-code-review
0@ 0 240k
Debug systematically to save time
Coding
HOT
Debug systematically to save time
systematic-debugging
0@ 0 240k
Write tests first, code with confidence
Coding
HOT
Write tests first, code with confidence
test-driven-development
0@ 0 240k
Build powerful MCP servers fast
Coding
HOT
Build powerful MCP servers fast
mcp-builder
0@ 1 156k
Transform messy data into clean spreadsheets
Coding
HOT
Transform messy data into clean spreadsheets
xlsx
0@ 0 156k