Memory safety patterns

From mtecoYY/ai-baike · View interactive version

Memory safety patterns is an AI agent skill from the GitHub repository mtecoYY/ai-baike. 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.

How to use this skill

Skill file

Memory Safety Patterns

Cross-language patterns for memory-safe programming including RAII, ownership, smart pointers, and resource management.

When to Use This Skill

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

Don'ts

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
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.