Implement memory-safe programming with RAII, ownership
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.
--- 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
Overview
Cross-language patterns for memory-safe programming including RAII, ownership, smart pointers, and resource management.
Manual install (npx, ZIP)
Claude Code
npx skills add wshobson/agents --skill memory-safety-patterns -g -a claude-codeClaude Code (manual)
mkdir -p ~/.claude/skills && curl -sL https://codeload.github.com/wshobson/agents/tar.gz/4236bb91f839 \
| tar -xz -C ~/.claude/skills --strip-components=4 agents-4236bb91f839/plugins/systems-programming/skills/memory-safety-patternsWindows (PowerShell)
New-Item -ItemType Directory -Force "$HOME\.claude\skills" | Out-Null; Invoke-WebRequest "https://m.agentalley.io/zip/b0cca95f393f25254bf7/memory-safety-patterns.zip" -OutFile "$env:TEMP\memory-safety-patterns.zip"; Expand-Archive "$env:TEMP\memory-safety-patterns.zip" -DestinationPath "$HOME\.claude\skills" -ForceClaude.ai / desktop
Codex, Cursor, other agents
Connects with Agent Alley MCP · agentalley.io/mcp
Files included
SKILL.md2.5 KBreferences/1 file · 10.8 KB
Read the files here
SKILL.md
Show the full text77 lines
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
| 1 | |
| 2 | name memory-safety-patterns |
| 3 | 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. |
| 4 | |
| 5 | |
| 6 | # Memory Safety Patterns |
| 7 | |
| 8 | Cross-language patterns for memory-safe programming including RAII, ownership, smart pointers, and resource management. |
| 9 | |
| 10 | ## When to Use This Skill |
| 11 | |
| 12 | Writing memory-safe systems code |
| 13 | Managing resources (files, sockets, memory) |
| 14 | Preventing use-after-free and leaks |
| 15 | Implementing RAII patterns |
| 16 | Choosing between languages for safety |
| 17 | Debugging memory issues |
| 18 | |
| 19 | ## Core Concepts |
| 20 | |
| 21 | ### 1. Memory Bug Categories |
| 22 | |
| 23 | | Bug Type | Description | Prevention | |
| 24 | | -------------------- | -------------------------------- | ----------------- | |
| 25 | | **Use-after-free** | Access freed memory | Ownership, RAII | |
| 26 | | **Double-free** | Free same memory twice | Smart pointers | |
| 27 | | **Memory leak** | Never free memory | RAII, GC | |
| 28 | | **Buffer overflow** | Write past buffer end | Bounds checking | |
| 29 | | **Dangling pointer** | Pointer to freed memory | Lifetime tracking | |
| 30 | | **Data race** | Concurrent unsynchronized access | Ownership, Sync | |
| 31 | |
| 32 | ### 2. Safety Spectrum |
| 33 | |
| 34 | |
| 35 | Manual (C) → Smart Pointers (C++) → Ownership (Rust) → GC (Go, Java) |
| 36 | Less safe More safe |
| 37 | More control Less control |
| 38 | |
| 39 | |
| 40 | ## Detailed patterns and worked examples |
| 41 | |
| 42 | Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient. |
| 43 | |
| 44 | ## Best Practices |
| 45 | |
| 46 | ### Do's |
| 47 | |
| 48 | **Prefer RAII** - Tie resource lifetime to scope |
| 49 | **Use smart pointers** - Avoid raw pointers in C++ |
| 50 | **Understand ownership** - Know who owns what |
| 51 | **Check bounds** - Use safe access methods |
| 52 | **Use tools** - AddressSanitizer, Valgrind, Miri |
| 53 | |
| 54 | ### Don'ts |
| 55 | |
| 56 | **Don't use raw pointers** - Unless interfacing with C |
| 57 | **Don't return local references** - Dangling pointer |
| 58 | **Don't ignore compiler warnings** - They catch bugs |
| 59 | **Don't use `unsafe` carelessly** - In Rust, minimize it |
| 60 | **Don't assume thread safety** - Be explicit |
| 61 | |
| 62 | ## Debugging Tools |
| 63 | |
| 64 | |
| 65 | # AddressSanitizer (Clang/GCC) |
| 66 | clang++ -fsanitize=address -g source.cpp |
| 67 | |
| 68 | # Valgrind |
| 69 | valgrind --leak-check=full ./program |
| 70 | |
| 71 | # Rust Miri (undefined behavior detector) |
| 72 | cargo +nightly miri run |
| 73 | |
| 74 | # ThreadSanitizer |
| 75 | clang++ -fsanitize=thread -g source.cpp |
| 76 | |
| 77 |
Details
- Updated
- Sep 21, 2026
- License
- MIT
- Stars on the repo
- ★ 39,857
- Checked
- Source
- wshobson/agents ↗
- Creator
- wshobson · 179 listings
Similar skills
See all →
SkillExtract text, tables, metadataXberg Document Extraction
SkillMove code from one implementation to another functionCode migration
SkillManages Ahrefs API usage in Python using ahrefs-python
MCPDocx LLM translatoradeu
SkillCreate an Amazon-compliant white-background main imageAmazon White-Background Main Image · 亚马逊白底主图
MCPOfficial monorepo of SDKs, CLISendmux sdkMore from wshobson
SkillMaster C/.NET backend development patterns for building
SkillImplement WCAG 2.2 compliant interfaces with mobileAccessibility compliance
SkillMaster Material Design 3 and Jetpack Compose patternsAndroid Mobile Design
SkillMigrate from AngularJS to Angular using hybrid modeAngular migration
SkillUnderstand anti-reversing, obfuscationAnti reversing techniques
SkillBuild production Apache Airflow DAGs with best practicesApache Airflow DAG Patterns