Memory Safety Patterns
Seth HobsonCommunityCreated by Seth Hobson
- Skill
- Guidelines
- Software development
- MIT
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a structured approach to memory safety by detailing cross-language patterns such as RAII, ownership, and smart pointers. It helps developers write robust systems code by explaining how to prevent common memory bugs like use-after-free, leaks, and data races. The content covers core concepts, best practices, and debugging strategies for C, C++, and Rust, offering a clear path to safer resource management.
What it does
- Defines core memory safety patterns like RAII and ownership.
- Categorizes common memory bugs and their prevention strategies.
- Provides C++ examples for smart pointers and RAII.
- Explains Rust ownership and borrowing concepts.
- Lists essential debugging tools for memory and thread safety.
- Offers best practices for avoiding raw pointers and dangling references.
When to use it
- When writing or refactoring systems-level code.
- When you need to manage complex resources like files or sockets.
- When debugging memory leaks or use-after-free errors.
- When choosing between languages based on their safety guarantees.
- When implementing RAII to ensure deterministic resource cleanup.
Advantages
- Reduces the risk of undefined behavior in systems programming.
- Provides clear, actionable patterns for multiple languages.
- Encourages modern, exception-safe coding practices.
- Helps bridge the gap between manual memory management and safe abstractions.
Usage examples
- “How can I prevent double-free errors in C++?”
- The skill explains using smart pointers and RAII to ensure resources are managed automatically, preventing manual free calls that lead to double-free bugs.
- “What is the best way to handle file handles safely?”
- It demonstrates the RAII pattern, where a class constructor opens the file and the destructor ensures it is closed, tying resource lifetime to object scope.
- “How do I debug memory leaks in my C++ application?”
- The skill provides commands for using Valgrind and AddressSanitizer to detect leaks and invalid memory access during runtime.
Install
Download the skill as a ZIP:
https://mp-staging.autogineer.com/agent-skills/wshobson/memory-safety-patterns/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip memory-safety-patterns.zip -d .agents/skills
Documentation by Seth Hobson
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 controlDetailed 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 itDon'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.cppFiles
- ATTRIBUTION.mdAdded by AutoGineer1004 B
- LICENSELicence file added by AutoGineer1.0 KiB
- SKILL.md2.5 KiB
- references/details.md10.8 KiB
LICENSE
MIT License Copyright (c) 2024 Seth Hobson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- Seth Hobson
- Publisher
- Seth Hobson (Community)
- Source
- wshobson/agents · plugins/systems-programming/skills/memory-safety-patterns · commit 4bde316
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by Seth Hobson.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download memory-safety-patterns.zip