Implement memory-safe programming with RAII, ownership

Memory safety patterns

by wshobson · MIT license · ★ 39,857 on GitHub · updated Sep 21

Memory safety patterns cover

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.

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

Overview

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

Manual install (npx, ZIP)
Claude Codenpx skills add wshobson/agents --skill memory-safety-patterns -g -a claude-code
Claude Code (manual)mkdir -p ~/.claude/skills && curl -sL https://codeload.github.com/wshobson/agents/tar.gz/4236bb91f839 \ | tar -xz -C ~/.claude/skills --strip-components=4 agents-4236bb91f839/plugins/systems-programming/skills/memory-safety-patterns
Windows (PowerShell)New-Item -ItemType Directory -Force "$HOME\.claude\skills" | Out-Null; Invoke-WebRequest "https://m.agentalley.io/zip/b0cca95f393f25254bf7/memory-safety-patterns.zip" -OutFile "$env:TEMP\memory-safety-patterns.zip"; Expand-Archive "$env:TEMP\memory-safety-patterns.zip" -DestinationPath "$HOME\.claude\skills" -Force
Claude.ai / desktopDownload the ZIP and upload it under Settings → Capabilities → Skills.
Codex, Cursor, other agentsPoint them at SKILL.md. The other files are plain text.

Connects with Agent Alley MCP · agentalley.io/mcp

Files included

  • SKILL.md2.5 KB
  • references/1 file · 10.8 KB
Read the files here
wshobson/4236bb92 files
SKILL.md
Show the full text77 lines

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
1---
2name: memory-safety-patterns
3description: 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.
4---
5 
6# Memory Safety Patterns
7 
8Cross-language patterns for memory-safe programming including RAII, ownership, smart pointers, and resource management.
9 
10## When to Use This Skill
11 
12- Writing memory-safe systems code
13- Managing resources (files, sockets, memory)
14- Preventing use-after-free and leaks
15- Implementing RAII patterns
16- Choosing between languages for safety
17- Debugging memory issues
18 
19## Core Concepts
20 
21### 1. Memory Bug Categories
22 
23| Bug Type | Description | Prevention |
24| -------------------- | -------------------------------- | ----------------- |
25| **Use-after-free** | Access freed memory | Ownership, RAII |
26| **Double-free** | Free same memory twice | Smart pointers |
27| **Memory leak** | Never free memory | RAII, GC |
28| **Buffer overflow** | Write past buffer end | Bounds checking |
29| **Dangling pointer** | Pointer to freed memory | Lifetime tracking |
30| **Data race** | Concurrent unsynchronized access | Ownership, Sync |
31 
32### 2. Safety Spectrum
33 
34```
35Manual (C) → Smart Pointers (C++) → Ownership (Rust) → GC (Go, Java)
36Less safe More safe
37More control Less control
38```
39 
40## Detailed patterns and worked examples
41 
42Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.
43 
44## Best Practices
45 
46### Do's
47 
48- **Prefer RAII** - Tie resource lifetime to scope
49- **Use smart pointers** - Avoid raw pointers in C++
50- **Understand ownership** - Know who owns what
51- **Check bounds** - Use safe access methods
52- **Use tools** - AddressSanitizer, Valgrind, Miri
53 
54### Don'ts
55 
56- **Don't use raw pointers** - Unless interfacing with C
57- **Don't return local references** - Dangling pointer
58- **Don't ignore compiler warnings** - They catch bugs
59- **Don't use `unsafe` carelessly** - In Rust, minimize it
60- **Don't assume thread safety** - Be explicit
61 
62## Debugging Tools
63 
64```bash
65# AddressSanitizer (Clang/GCC)
66clang++ -fsanitize=address -g source.cpp
67 
68# Valgrind
69valgrind --leak-check=full ./program
70 
71# Rust Miri (undefined behavior detector)
72cargo +nightly miri run
73 
74# ThreadSanitizer
75clang++ -fsanitize=thread -g source.cpp
76```
77 
View all files on GitHub →

Details

Updated
Sep 21, 2026
License
MIT
Stars on the repo
★ 39,857
Checked
Source
wshobson/agents ↗
Creator
wshobson · 179 listings
View creator

Similar skills

See all →
SkillExtract text, tables, metadataXberg Document Extractionxberg-io/xberg · by xberg-ioExtract text, tables, metadata, and images from 107 document formats (PDF, Office, images, HTML, email, archives, academic) using Xberg. Use when writing code that calls Xberg APIs in Python, Node.js/TypeScript, Rust, or CLI. Covers installation, extraction (sync/async), configuration (OCR, chunking, output format), batch processing, error handling, and plugins.★ 9.4k GitHub starsSkillMove code from one implementation to another functionCode migrationkucherenko/jscpd · by kucherenkoMove code from one implementation to another function by function, tests first and code second, and check two implementations of one app for parity, with jscpd --compare as the progress measure and a coverage map binding each function to its tests. Use when porting a library or app to another language or framework (Java to Kotlin, JavaScript to Rust, a Python library to TypeScript, an iOS app to Android), when asked what is left to port, or when comparing the Android and iOS versions of an app.★ 6.3k GitHub starsSkillManages Ahrefs API usage in Python using ahrefs-pythonOpenClaudia/openclaudia-skills · by OpenClaudiaManages Ahrefs API usage in Python using `ahrefs-python` library. Use when working with SEO / marketing related tasks or with data including backlinks, keywords, domain ratings, organic traffic, site audits, rank tracking, and brand monitoring. Covers `ahrefs-python` usage including AhrefsClient / AsyncAhrefsClient, typed request/response models, error handling, and all API sections.★ 705 GitHub starsMCPDocx LLM translatoradeudealfluence/adeu · by dealfluenceDocx ↔ LLM translator. Projects .docx office files to Markdown for editing. Projects edits back to OOXML as tracked changes (redlines). Python and Node.js implementations.★ 165 GitHub starsSkillCreate an Amazon-compliant white-background main imageAmazon White-Background Main Image · 亚马逊白底主图xianyu110/ecommerce-image-skills · by xianyu110Create an Amazon-compliant white-background main image (pure #FFFFFF background, product fills ≥85% of the frame, no text, props or watermark) from a product photo with GPT Image 2.5, then verify it with a Python check script. Use for Amazon/Walmart/eBay main images, 白底图, 白底主图, hero image, listing main photo.★ 92 GitHub starsMCPOfficial monorepo of SDKs, CLISendmux sdkSendmux/sendmux-sdk · by SendmuxOfficial monorepo of SDKs, CLI, and MCP servers for Sendmux email APIs across TypeScript, Python, Go, PHP, Rust, and Ruby.★ 65 GitHub stars

More from wshobson

SkillMaster C/.NET backend development patterns for buildingwshobson/agents · by wshobsonMaster C#/.NET backend development patterns for building robust APIs, MCP servers, and enterprise applications. Covers async/await, dependency injection, Entity Framework Core, Dapper, configuration, caching, and testing with xUnit. Use when developing .NET backends, reviewing C# code, or designing API architectures.★ 39.9k GitHub starsSkillImplement WCAG 2.2 compliant interfaces with mobileAccessibility compliancewshobson/agents · by wshobsonImplement WCAG 2.2 compliant interfaces with mobile accessibility, inclusive design patterns, and assistive technology support. Use when auditing accessibility, implementing ARIA patterns, building for screen readers, or ensuring inclusive user experiences.★ 39.9k GitHub starsSkillMaster Material Design 3 and Jetpack Compose patternsAndroid Mobile Designwshobson/agents · by wshobsonMaster Material Design 3 and Jetpack Compose patterns for building native Android apps. Use when designing Android interfaces, implementing Compose UI, or following Google's Material Design guidelines.★ 39.9k GitHub starsSkillMigrate from AngularJS to Angular using hybrid modeAngular migrationwshobson/agents · by wshobsonMigrate from AngularJS to Angular using hybrid mode, incremental component rewriting, and dependency injection updates. Use when upgrading AngularJS applications, planning framework migrations, or modernizing legacy Angular code.★ 39.9k GitHub starsSkillUnderstand anti-reversing, obfuscationAnti reversing techniqueswshobson/agents · by wshobsonUnderstand anti-reversing, obfuscation, and protection techniques encountered during software analysis. Use this skill when analyzing malware evasion techniques, when implementing anti-debugging protections for CTF challenges, when reverse engineering packed binaries, or when building security research tools that need to detect virtualized environments.★ 39.9k GitHub starsSkillBuild production Apache Airflow DAGs with best practicesApache Airflow DAG Patternswshobson/agents · by wshobsonBuild production Apache Airflow DAGs with best practices for operators, sensors, testing, and deployment. Use when creating data pipelines, orchestrating workflows, or scheduling batch jobs.★ 39.9k GitHub stars