objc-block-retain-cycles

Diagnose Objective-C block retain cycles using Instruments and the weak-strong pattern.

1.1k|81|Updated Nov 30, 2025
One-click install
npx skills add https://github.com/CharlesWiltgen/Axiom --skill objc-block-retain-cycles
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: objc-block-retain-cycles
Source: https://github.com/CharlesWiltgen/Axiom/tree/main/plugins/axiom/skills/objc-block-retain-cycles
Command: npx skills add https://github.com/CharlesWiltgen/Axiom --skill objc-block-retain-cycles

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides a systematic approach to diagnose and fix Objective-C block retain cycles, the leading cause of memory leaks, app crashes, and "sending message to deallocated instance" errors. It prevents silent memory accumulation and ensures app stability.

Core Features & Use Cases

  • Systematic Diagnosis: Mandatory first steps using Instruments (Allocations, Memory Debugger) to confirm, trace, and understand retain cycles.
  • Weak-Strong Pattern Enforcement: Detailed guidance on correctly applying the __weak typeof(self) weakSelf = self; and if (strongSelf) pattern to break cycles.
  • Hidden Capture Detection: Identifies subtle self captures within macros (e.g., NSAssert, NSLog) and ensures proper guarding in nested blocks.
  • Use Case: When a UIViewController isn't deallocating after being dismissed, use this skill to systematically check for self references in any blocks assigned to properties or network operations, then apply the weak-strong pattern to break the cycle.

Quick Start

To diagnose a suspected block retain cycle, first use Instruments' Allocations template to confirm memory isn't returning to baseline after dismissing a screen.

Frequently Asked Questions about objc-block-retain-cycles

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I identify block retain cycles causing memory leaks in Objective-C?

Block retain cycles occur when a block captures `self` and is retained by a property or assignment on that same object, creating a circular reference. Use Instruments' Allocations template to confirm memory isn't returning to baseline after dismissing a screen, then trace the cycle using the Memory Debugger to locate retained blocks.

What is the weak-strong pattern and how do I apply it to fix block retain cycles?

The weak-strong pattern breaks cycles by capturing a weak reference to `self` before the block executes. Declare `__weak typeof(self) weakSelf = self;` outside the block, then inside use `typeof(self) strongSelf = weakSelf;` with an `if (strongSelf)` guard to safely access self without creating a retain cycle.

Why does my UIViewController not deallocate after dismissing it?

A UIViewController fails to deallocate when blocks assigned to properties or used in network callbacks capture `self` without the weak-strong pattern, creating a retain cycle. Systematically check for `self` references in all blocks—including hidden captures in macros like `NSAssert` or `NSLog`—and apply weak-strong guarding to break the cycle.

Can I detect hidden self captures in macros when using blocks?

Yes. Hidden `self` captures occur within macros like `NSAssert` and `NSLog` inside blocks. Use Instruments' Memory Debugger to trace retained instances and review macro expansions, then apply the weak-strong pattern to guard all block code, including macro invocations.

Do I need to run block retain cycle diagnostics on a real device?

Yes. Real-device testing is mandatory for block retain cycle diagnosis because simulator memory behavior differs from production. Use Instruments on a physical device to confirm memory behavior after screen dismissals and validate that the weak-strong pattern actually breaks the cycle.

When should I use Instruments to diagnose block retain cycles?

Use Instruments when you observe memory not returning to baseline after dismissing screens, encounter "sending message to deallocated instance" crashes, or suspect silent memory accumulation from network callbacks or asynchronous operations. Start with Allocations to confirm the leak, then use Memory Debugger to trace and identify retained blocks.