Architectural Invariant Design: Kotlin Multiplatform Swift Export Weak-Reference Bridging & Retain Cycle Elimination
Kotlin Multiplatform Swift Export Weak-Reference Bridging & Retain Cycle Elimination defines an industrial-grade behavioral standard for mobile engineering. Defines strict architectural boundaries between Apple Automatic Reference Counting (ARC) and Kotlin Garbage Collection (GC) runtimes across Swift Export. Eliminates cross-runtime bidirectional retain cycles and non-deterministic native deallocation by mandating weak-reference wrappers, explicit lifecycle unbinding, and decoupled observer interfaces. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.
Hazard Analysis: What Breaks Without This Rule
The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:
| Failure Mode | AI Symptom (The Anti-Pattern) | Severity | Production Impact & Remedy |
|---|---|---|---|
| AI Hallucination & Deprecation Drift | Bidirectional Hard References Between Swift Closures/Delegates and Kotlin GC Objects Inducing Native-to-GC Retain Cycles and Memory Leaks | Critical | Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production. Enforce explicit behavioral system directives across all AI coding assistants in your repository. |
| Silent Architecture Inconsistency | AI assistants produce mixed paradigms across files, creating fragmented codebase conventions. | Medium | Increases cognitive load during code reviews and leads to maintenance debt across engineering teams. Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails. |
Step-by-Step Production Directives
Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:
Engineers MUST NEVER maintain a strong reference to a Swift class or native Swift callback delegate inside a long-lived Kotlin heap object without using a weak reference wrapper (e.g., `kotlin.native.ref.WeakReference` or native interop weak storage).
Engineers MUST NEVER maintain a strong reference to a Swift class or native Swift callback delegate inside a long-lived Kotlin heap object without using a weak reference wrapper (e.g., `kotlin.native.ref.WeakReference` or native interop weak storage).
Engineers MUST ALWAYS expose explicit lifecycle termination hooks (`detach()`, `dispose()`, `clearListener()`) on long-lived Kotlin presenters, view models, or domain managers that hold native delegates.
Engineers MUST ALWAYS expose explicit lifecycle termination hooks (`detach()`, `dispose()`, `clearListener()`) on long-lived Kotlin presenters, view models, or domain managers that hold native delegates.
Swift clients bridging callbacks to Kotlin MUST ALWAYS declare capture lists with `[weak self]` or `[unowned self]` when forwarding Swift closures into Kotlin interfaces.
Swift clients bridging callbacks to Kotlin MUST ALWAYS declare capture lists with `[weak self]` or `[unowned self]` when forwarding Swift closures into Kotlin interfaces.
Engineers MUST NOT allow native platform objects (such as `UIViewController` or `UIView`) to implement Kotlin observer interfaces directly if the Kotlin object is injected into or owned by that native platform object.
Engineers MUST NOT allow native platform objects (such as `UIViewController` or `UIView`) to implement Kotlin observer interfaces directly if the Kotlin object is injected into or owned by that native platform object.
Kotlin Multiplatform code bridging asynchronous state via callbacks MUST store listener collections using explicit weak reference wrappers or explicitly unregister observers during native deinit.
Kotlin Multiplatform code bridging asynchronous state via callbacks MUST store listener collections using explicit weak reference wrappers or explicitly unregister observers during native deinit.
When compiling Kotlin Native with Swift Export, engineers MUST ensure any native callback abstraction allows the underlying target to be deallocated independently of the Kotlin GC collection cycle.
When compiling Kotlin Native with Swift Export, engineers MUST ensure any native callback abstraction allows the underlying target to be deallocated independently of the Kotlin GC collection cycle.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Kotlin Multiplatform Swift Export Weak-Reference Bridging & Retain Cycle Elimination critical for AI coding assistants?
Defines strict architectural boundaries between Apple Automatic Reference Counting (ARC) and Kotlin Garbage Collection (GC) runtimes across Swift Export. Eliminates cross-runtime bidirectional retain cycles and non-deterministic native deallocation by mandating weak-reference wrappers, explicit lifecycle unbinding, and decoupled observer interfaces. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.
Q:How does adopting this rule prevent AI hallucinations?
This rule explicitly declares what the AI must avoid (Bidirectional Hard References Between Swift Closures/Delegates and Kotlin GC Objects Inducing Native-to-GC Retain Cycles and Memory Leaks) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.
Q:Which AI coding environments are supported?
This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).
Q:Can I combine this rule with other technology stacks?
Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.