Apple & iOS
AuraScore 79/100

Swift Memory Leak and Retain Cycle Root Cause Analysis

Diagnose complex ARC retain cycles, memory graph anomalies, and closure capture leaks across iOS application architectures.

Use this template when tracking down stubborn memory spikes, Instruments leaks, or deallocation failures in Swift codebases. It isolates retain cycles and maps out direct remediation code fixes.

Template

Role: Principal iOS Performance Engineer specializing in Swift memory management, Instruments, and ARC diagnostics.

Context

  • Target application module: {{target_module}}
  • User workflow triggering the issue: {{reproduction_scenario}}
  • Summary of Instruments trace data: {{instruments_trace_summary}}
  • Suspected source code implementation: {{suspect_code_snippet}}
  • Core architectural pattern in use: {{app_architecture_pattern}}
  • Target deployment OS baseline: {{target_os_version}}

Task

Produce an exhaustive memory graph analysis and technical remediation breakdown for {{target_module}} that isolates the exact ARC retain cycle or deallocation failure occurring during {{reproduction_scenario}} and delivers precise refactoring recommendations.

Method

  1. Analyze {{suspect_code_snippet}} in the context of {{app_architecture_pattern}} to identify strong reference cycles between owners, delegates, and closures.
  2. Cross-reference the memory footprint anomalies described in {{instruments_trace_summary}} against standard Swift allocation behaviors on {{target_os_version}}.
  3. Map out the complete ownership graph, charting every strong, weak, and unowned reference path associated with {{reproduction_scenario}}.
  4. Evaluate closure capture lists, escaping callbacks, and notification center observations for unreleased scope captures.
  5. Trace SwiftUI or UIKit lifecycle bindings to determine if state objects or coordinators survive view dismissals.
  6. Formulate a step-by-step code patch that breaks all cyclic dependencies without introducing dangling pointers.
  7. Define an Instruments verification protocol to confirm zero persistent allocations post-dismissal.

Constraints

  • MUST cite specific object instances and closure capture semantics causing the leak.
  • MUST provide production-ready Swift code demonstrating the exact fix.
  • MUST NOT recommend blanket usage of unowned references without explicit lifecycle proof.
  • Avoid speculative third-party framework issues unless directly supported by {{instruments_trace_summary}}.

Output format

  • Executive Root Cause Summary (1 paragraph)
  • Object Ownership & Reference Graph (ASCII diagram or bulleted hierarchy)
  • Technical Flaw Breakdown (detailed code-level walk-through)
  • Refactored Swift Code Implementation (unified diff or clean code block)
  • Instruments Validation & Profiling Recipe (3-5 step verification guide)

Self-review

  • Did I account for all variables including {{target_os_version}} and {{app_architecture_pattern}}?
  • Are all proposed capture list modifications syntactically valid in modern Swift?
  • Does the remediation guarantee complete deallocation upon view teardown?
AuraScore breakdown
79/100Provisional
Instruction clarity15/15 · Strong

Explicit role, a named task, and discrete steps the model can follow.

Context architecture12/12 · Strong

Background, inputs and variables the model needs before it starts.

Constraint engineering10/12 · Adequate

Hard boundaries — what the model must and must not do.

Output specification6/14 · Thin

A named, field-level shape for the response.

Reasoning structure10/10 · Strong

Ordered work items that force analysis before an answer.

Model compatibility10/10 · Strong

Length and structure that travel across frontier models.

Token efficiency5/10 · Thin

Signal density — instruction weight without padding.

Reusability7/7 · Strong

Documented variables so the scaffold adapts to new inputs.

Robustness3/5 · Adequate

Quality bar, assumptions and behaviour when inputs are thin.

Observed performance1/5 · Thin

How much real usage the template has behind it.

developers
developers-apple
software-engineering-debugging
ios
swift
performance