---
title: "メモリを安全に管理する（エージェント用スキル） | Orchestor"
description: "RAII、所有権、スマートポインタ、リソース管理を使い、Rust、C++、C でメモリ安全性を確保します。安全なシステムコードの作成、リソース管理、メモリ関連の不具合の予防に使います。wshobson/agents が公開している「メモリを安全に管理する」の使い方を確認できます。"
canonical_url: "https://www.orchestor.io/ja/skills/wshobson/agents/memory-safety-patterns"
markdown_url: "https://www.orchestor.io/ja/skills/wshobson/agents/memory-safety-patterns.md"
---

[c96b5f6](https://github.com/wshobson/agents/commit/c96b5f6552dba32e4492307493ee613581430715)[原文を開く](https://github.com/wshobson/agents/blob/c96b5f6552dba32e4492307493ee613581430715/plugins/systems-programming/skills/memory-safety-patterns/SKILL.md)

wshobson2026/11/24

原文 (選択中)

未翻訳

プレビュー (選択中)

76 行（空行を除く 55 行） · 2.53 KB原文

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

```bash
# 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
```

## Structured data

```json
[
  [
    {
      "@context": "https://schema.org",
      "@type": "Organization",
      "name": "Orchestor",
      "url": "https://www.orchestor.io",
      "sameAs": [
        "https://orchestor.io",
        "https://www.orchestor.io",
        "https://docs.orchestor.io"
      ],
      "@id": "https://www.orchestor.io/#organization",
      "alternateName": [
        "オーケスター"
      ],
      "logo": "https://www.orchestor.io/images/common/ogp/orchestor-ogp.png",
      "parentOrganization": {
        "@type": "Organization",
        "name": "ドットメディア株式会社",
        "url": "https://www.dotmedia.co.jp"
      }
    },
    {
      "@context": "https://schema.org",
      "@type": "WebSite",
      "url": "https://www.orchestor.io",
      "name": "Orchestor",
      "description": "Orchestor で画像・動画・音声を作り、参照素材を Remix できます。",
      "@id": "https://www.orchestor.io/#website",
      "alternateName": [
        "オーケスター"
      ],
      "inLanguage": "ja",
      "publisher": {
        "@id": "https://www.orchestor.io/#organization"
      }
    }
  ],
  {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    "@id": "https://www.orchestor.io/skills/wshobson/agents/memory-safety-patterns#breadcrumb",
    "itemListElement": [
      {
        "@type": "ListItem",
        "position": 1,
        "name": "Orchestor",
        "item": "https://www.orchestor.io/"
      },
      {
        "@type": "ListItem",
        "position": 2,
        "name": "スキル",
        "item": "https://www.orchestor.io/skills"
      },
      {
        "@type": "ListItem",
        "position": 3,
        "name": "メモリを安全に管理する",
        "item": "https://www.orchestor.io/skills/wshobson/agents/memory-safety-patterns"
      }
    ]
  }
]
```

---

[Documentation index](https://www.orchestor.io/llms.txt)
