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- 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
How to use memory-safety-patterns
- 1.Invoke the skill when writing or reviewing systems code in Rust, C++, or C that involves resource or memory management
- 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.Apply the recommended pattern: RAII for resource lifetimes, smart pointers in C++, ownership/borrowing in Rust
- 4.Follow the Do's and Don'ts list when writing or refactoring code
- 5.Consult references/details.md for detailed patterns and worked examples when the top-level guidance isn't enough
- 6.Run the relevant debugging tool (AddressSanitizer, Valgrind, Rust Miri, or ThreadSanitizer) using the provided commands to verify memory safety
Use cases
- 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
- 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
Rust, C++, and C, with comparisons across their safety models (manual memory in C, smart pointers in C++, ownership in Rust).
No. It provides patterns, guidance, and tool commands (AddressSanitizer, Valgrind, Miri, ThreadSanitizer) to help you write safe code and detect issues yourself.
Use-after-free, double-free, memory leaks, buffer overflows, dangling pointers, and data races, along with prevention techniques for each.
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 Type | Description | Prevention |
|---|---|---|
| Use-after-free | Access freed memory | Ownership, RAII |
| Double-free | Free same memory twice | Smart pointers |
| Memory leak | Never free memory | RAII, GC |
| Buffer overflow | Write past buffer end | Bounds checking |
| Dangling pointer | Pointer to freed memory | Lifetime tracking |
| Data race | Concurrent unsynchronized access | Ownership, 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
unsafecarelessly - 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
Related skills
More from wshobson/agents and the wider catalog.
tailwind-design-system
Build production-ready design systems with Tailwind CSS v4, design tokens, and component libraries.
typescript-advanced-types
Master TypeScript's advanced type system: generics, conditional types, mapped types, and utility types for type-safe applications.
nodejs-backend-patterns
Build production-ready Node.js backends with Express/Fastify, middleware patterns, auth, and database integration.
python-performance-optimization
Profile and optimize Python code using cProfile, memory profilers, and performance best practices.
brand-landingpage
Brand-first landing page designer with guided interviews and Stitch-powered iteration.
python-testing-patterns
Implement comprehensive testing strategies with pytest, fixtures, mocking, and test-driven development.