Skip to content
Back to skills

Swift Memory Management

ASecurity

Prevent retain cycles via ARC, weak/unowned references, and Capture Lists in Swift. Use when managing Swift ARC, avoiding retain cycles, or configuring capture lists in closures.

  • 549 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added June 6, 2026
developmentswiftapi

Works with

  • api

Security analysis

A100/100

Pro scans all 3 files and shows the line behind each finding

Scanned June 6, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill swift-memory-management --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Swift Memory Management?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Swift Memory Management
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-swift-memory-management/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-swift-memory-management)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: swift-memory-management
description: Prevent retain cycles via ARC, weak/unowned references, and Capture Lists in Swift. Use when managing Swift ARC, avoiding retain cycles, or configuring capture lists in closures.
metadata:
  triggers:
    files:
    - '**/*.swift'
    keywords:
    - weak
    - unowned
    - capture
    - deinit
    - retain
---
# Swift Memory Management

## **Priority: P0**

## Implementation Guidelines

### ARC Fundamentals

- **Default**: Strong references. ARC handles retain/release automatically.
- **Weak**: Use weak if the reference can become nil during its lifetime (delegates, optional parent refs).
- **Unowned**: Use unowned if the reference is guaranteed to outlive referring object (rare; prefer `weak`).

### Capture Lists

- **Closures**: `[weak self]` at beginning of the closure's capture list. Pattern: `{ [weak self] in guard let self = self else { return } }`.
- **Self in Structs**: No list needed — `self` copied by value.
- **Multiple Captures**: `[weak self, weak delegate]`.

### Retain Cycles

- **Delegates**: `weak var delegate` always. Protocol inherits from AnyObject (e.g., `protocol MyDelegate: AnyObject {}`)..
- **Closures as Properties**: Use `weak` or `unowned` in capture list.
- **Two-way References**: One side must `weak`.

## Anti-Patterns

- **No strong var delegate**: Use weak.
- **No self in escaping closures**: Use [weak self].
- **No unowned unless certain**: Default to weak to prevent crashes.

## References

- [Capture Lists & Retain Cycles](references/implementation.md)

Files in this skill

  • SKILL.md1.5 KB
  • evals/evals.json2 KB
  • references/implementation.md1.3 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.