home / skills / sickn33 / antigravity-awesome-skills / memory-safety-patterns

memory-safety-patterns skill

/skills/memory-safety-patterns

This skill helps you apply memory-safe patterns across Rust, C++, and C, ensuring RAII, ownership, and resource management.

This is most likely a fork of the memory-safety-patterns skill from xfstudio
npx playbooks add skill sickn33/antigravity-awesome-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 for systems programming using RAII, ownership models, smart pointers, and disciplined resource management across Rust, C++, and C. It focuses on preventing leaks, use-after-free, and other common memory bugs while offering actionable guidance for design and debugging. The guidance is practical and targeted to engineers writing high-performance, reliable code.

How this skill works

I inspect the problem context, resource lifetimes, and existing ownership semantics, then recommend language-appropriate patterns (RAII in C++, ownership and borrowing in Rust, disciplined APIs in C). I provide concrete refactor steps, safe API sketches, and verification methods like static analysis, sanitizers, and unit tests. When requested, I map equivalent patterns across languages to help choose the safest implementation.

When to use it

  • Designing or refactoring systems code that manages memory, files, sockets, or other resources
  • Implementing RAII, smart pointer policies, or ownership models in C++ and Rust
  • Porting low-level C code to safer abstractions or auditing C code for leaks and use-after-free
  • Creating APIs that must enforce clear lifetime and ownership contracts
  • Debugging intermittent memory corruption or performance issues caused by improper resource handling

Best practices

  • Prefer language-native ownership/RAII features (Rust ownership/borrowing, C++ RAII and smart pointers) over manual management
  • Encapsulate resource acquisition and release in single objects to avoid scattered cleanup logic
  • Use static analyzers and sanitizers (ASan, Valgrind, Miri) early and in CI to catch lifetime errors
  • Design APIs with explicit ownership transfer semantics (move, borrow, reference-counted) and document invariants
  • Minimize raw pointer exposure; when unavoidable, wrap them with safe utility functions and thorough tests

Example use cases

  • Refactor a legacy C module that opens files and sockets into a C++ RAII wrapper to guarantee close-on-destruction
  • Audit a mixed Rust/C++ codebase to replace manual reference counting with Arc/Mutex or Rust ownership where possible
  • Design a safe FFI boundary that enforces ownership transfer rules when calling into C from Rust
  • Create unit and integration tests combined with sanitizers to reproduce and fix a use-after-free bug
  • Choose between unique_ptr, shared_ptr, and custom scoped handles in C++ based on lifetime and performance constraints

FAQ

Can these patterns be applied incrementally to a large codebase?

Yes. Start by isolating resource-handling hotspots, add RAII wrappers or ownership types for those modules, and expand coverage while running sanitizers and tests.

How do I choose between smart pointers in C++?

Use unique ownership (unique_ptr) when a single owner exists, reference-counted (shared_ptr) when ownership must be shared, and raw pointers only for non-owning references. Prefer move semantics to transfer ownership.