memory-safety-patterns
What it does
Helps write computer code that manages memory safely, avoiding common errors like leaks or using memory that has already been freed. This is done using techniques like RAII, smart pointers, and ownership.
When to use it
- ✓When creating programs that need to be very reliable (operating systems, games).
- ✓When you want to prevent your program from crashing or behaving strangely due to memory errors.
- ✓When learning or using languages like C++, Rust, or C to ensure safety.
Skill link
https://github.com/wshobson/agents/blob/main/plugins/systems-programming/skills/memory-safety-patterns/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agent’s skills folder.
Example of what to ask your AI
“How can I prevent memory leaks in C++ using RAII?”
5 new skills a week. 1 minute to read.
How it works
This tool teaches programming patterns that tie the lifetime of resources (like memory or files) to the lifetime of objects. When an object is created, it acquires a resource; when the object is destroyed, it automatically releases the resource.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
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
FAQ
What is RAII?
RAII stands for 'Resource Acquisition Is Initialization'. It's a technique where acquiring a resource (like allocating memory) happens during object initialization, and releasing it happens when the object is destroyed.
What are smart pointers?
These are objects that act like pointers but automatically manage the memory they point to, helping to prevent errors like leaks or accessing invalid memory.
What's the difference between C and Rust regarding memory safety?
C requires the programmer to manage memory manually, which is error-prone. Rust uses an 'ownership' system that guarantees memory safety at compile time, without needing a garbage collector.
Details
Original description by 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.
repo: wshobson/agents
Popularity
- GitHub stars
- 41k
- Ranking position
- #6799
- License
- MIT
- Updated
- 10/15/2026
Comments
- …
More skills from this repository
screen-reader-testing
41kHas your agent test websites using screen readers to ensure they are accessible.
wcag-audit-patterns
41kHelps your agent audit websites for accessibility and fix issues according to WCAG guidelines.
multi-reviewer-patterns
41kEnables your agent to coordinate parallel code reviews, deduplicating findings and consolidating reports.
parallel-debugging
41kAllows the agent to debug complex issues with parallel investigations and root cause analysis.
Similar skills
diffs
394kLets your agent produce a real, shareable diff of edits as a viewer link or image file, instead of a hand-written summary.
coding-agent
394kHelps your agent hand a larger coding job to Codex, Claude Code, or OpenCode as a background worker, then check on its progress.
gh-issues
394kHelps your agent find GitHub issues, pick the ones to work on, and open pull requests with fixes, including follow-up on review comments.