home / skills / xfstudio / skills / memory-safety-patterns

memory-safety-patterns skill

/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-patterns

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
1.1 KB
---
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.

Overview

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.

How this skill works

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.

When to use it

  • Writing low-level or systems code where memory safety matters
  • Managing resources like files, sockets, threads, and heap allocations
  • Preventing use-after-free, double-free, or memory leak bugs
  • Choosing or justifying language features for safer code (Rust vs C++ vs C)
  • Designing APIs with clear ownership and lifetime semantics

Best practices

  • Clarify goals and constraints before applying patterns (performance, API shape, FFI)
  • Prefer language-native safety features: ownership/borrowing in Rust, RAII in C++, explicit ownership in C
  • Encapsulate resources in single-responsibility types that manage acquire/release
  • Document ownership transfer and aliasing rules in APIs and code comments
  • Add automated checks: unit tests, sanitizers, and leak detectors; validate with continuous testing

Example use cases

  • Refactor a C ABI to a safe wrapper that uses RAII-style resource handles
  • Design a C++ class hierarchy using unique_ptr/shared_ptr with clear ownership rules
  • Map Rust ownership patterns to a C FFI boundary to avoid dangling pointers
  • Create a resource management checklist for a systems project before audits
  • Produce verification steps: sanitizer runs, stress tests, and lifetime assertions

FAQ

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.