home / skills / xfstudio / skills / memory-safety-patterns
This skill helps you apply memory safety patterns across Rust, C++, and C to prevent leaks, use-after-free, and improve resource management.
npx playbooks add skill xfstudio/skills --skill memory-safety-patternsReview the files below or copy the command above to add this skill to your agents.
---
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.
## Use this skill when
- 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
## Do not use this skill when
- The task is unrelated to memory safety patterns
- You need a different domain or tool outside this scope
## Instructions
- Clarify goals, constraints, and required inputs.
- Apply relevant best practices and validate outcomes.
- Provide actionable steps and verification.
- If detailed examples are required, open `resources/implementation-playbook.md`.
## Resources
- `resources/implementation-playbook.md` for detailed patterns and examples.
This skill teaches cross-language memory-safety patterns focused on RAII, ownership, smart pointers, and resource management across Rust, C++, and C. It helps you design and implement resource-safe code to avoid leaks, use-after-free, and other memory bugs. The guidance is practical and targeted to systems programming scenarios.
The skill inspects code intent and constraints, then recommends language-appropriate patterns: ownership and borrow rules for Rust, RAII and smart-pointer idioms for C++, and disciplined resource wrappers for C. It produces concrete refactor steps, code templates, and verification checks such as ownership diagrams, lifetime annotations, and leak detection strategies. You can request focused examples or a full implementation playbook for deeper patterns.
Which language should I choose for maximum memory safety?
Use Rust for strong static guarantees and borrow checking; use C++ with disciplined RAII and modern smart pointers if performance or ecosystem requires it. C needs wrappers and conventions to reach similar safety levels.
Can these patterns eliminate all memory bugs?
They greatly reduce classes of bugs but cannot eliminate logic errors or concurrency races; combine patterns with testing, sanitizers, and code review for best results.