memory-safety-patterns
Enables your agent to write safer code in languages like Rust and C++. It prevents common memory errors, ensuring software stability and reliability.
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.
Example of what to ask your AI
“How can I prevent memory leaks in C++ using RAII?”
Want more? Open the sections below.
How to use
The easiest way: copy the message below and paste it into Claude, ChatGPT, Cursor or any AI. It reads the skill and follows the instructions.
Skill link
Ready-to-paste message
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.
Installation
Easiest way: copy the skill link and paste it into Claude, ChatGPT or Cursor asking it to follow the instructions.
In the terminal (Claude Code, Codex, Cursor), in your project folder:
npx skills add wshobson/agents --skill memory-safety-patternsOr download just the file:
curl -L https://raw.githubusercontent.com/wshobson/agents/main/plugins/systems-programming/skills/memory-safety-patterns/SKILL.md -o SKILL.mdTechnical 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
- #4966
- License
- MIT
- Updated
- 10/15/2026
Comments
- …
More skills from this repository
screen-reader-testing
Has your agent test websites using screen readers to ensure they are accessible.
wshobson/agents
wcag-audit-patterns
Helps your agent audit websites for accessibility and fix issues according to WCAG guidelines.
wshobson/agents
multi-reviewer-patterns
Enables your agent to coordinate parallel code reviews, deduplicating findings and consolidating reports.
wshobson/agents
parallel-debugging
Allows the agent to debug complex issues with parallel investigations and root cause analysis.
wshobson/agents
Similar skills
benchmark-optimization-loop
Has your agent improve something's speed in a measured way. It tests multiple versions and chooses the fastest one that still works correctly.
affaan-m/ECC
change-impact
Helps your agent analyze the impact of code changes before making them. It compares the planned vs. actual results afterward, covering paths, data, tests, and documentation. Useful for risky or system-wide changes.
affaan-m/ECC
click-path-audit
Lets your agent trace all user buttons and interactions to find bugs. It finds issues where functions seem to work alone but fail together. Useful when bugs persist even after debugging.
affaan-m/ECC