Skills wshobson agents

memory-safety-patterns

Use it when:
You're writing or reviewing C, C++ or Rust and want to rule out leaks, dangling pointers, buffer overflows and data races.
It does:
Gives the agent a table of six memory bug types and how each is prevented, with code patterns for each language.
You get:
Code written along those patterns; it adds no steps or files of its own.
Terms:
RAII ties a resource to a scope, so it's freed when the scope ends; ownership is Rust's compile-time rule of one owner per value.
Debugging tools:
Commands for AddressSanitizer, ThreadSanitizer, Valgrind and Rust's Miri.

TL;DR

A reference, not a workflow. It places languages on a spectrum from manual memory management in C, through smart pointers in C++ and ownership in Rust, to garbage collection in Go and Java: safer toward the end, with less control. A reference file holds six patterns with code: RAII classes in C++ for files, locks and a transaction that rolls back unless committed; unique_ptr, shared_ptr and weak_ptr, with make_unique over raw new; Rust's moves, borrowing, lifetimes, interior mutability and Rc and Arc for shared ownership; cleanup functions and the GCC/Clang cleanup attribute in C, which has no RAII; bounds checking with containers, at() and std::span in C++ and slices in Rust; and data-race prevention with atomics, mutexes and read-write locks in both. Its rules include avoiding raw pointers except at C boundaries, never returning references to locals, keeping Rust's unsafe to a minimum and never assuming thread safety.

Summarizes the skill at 4bde316.

npx skills add https://github.com/wshobson/agents --skill memory-safety-patterns
Total Downloads
12,200 on skills.sh, Nov 9, 2026
Repo stars
42,334 wshobson/agents
First seen
Jan 20, 2026 on skills.sh
Weekly Downloads
165
-15% over the 8 weeks to Nov 9
Weekly values
Week toInstalls
Sep 21194
Sep 28227
Oct 5284
Oct 12223
Oct 19214
Oct 26205
Nov 2189
Nov 9165
Category

Test-first work, implementation from specs, git hygiene and scope control.

Everything for building software: planning, coding, debugging, design, frameworks, cloud, agents and skills.

Type

Knowledge and rules the agent applies while it works: best practices, design principles, API references.

Effect

Adds no side effects of its own. It shapes how the agent thinks, writes or reviews. Guidelines and routers are always effect:none, even when the task they support edits code.

Needs

Instructions only. Works in any agent that supports skills.

Publisher

Everyone else. Unverified vendor claims default to community.

Topics
Stack

SKILL.md

From wshobson/agents at 4bde316. Checked daily, last changed Nov 6, 2026.

Frontmatter
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

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 unsafe carelessly - 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