Skip to content

fix(iOS): memory leak caused by retain cycle in layout manager and input parser#513

Merged
kacperzolkiewski merged 2 commits intosoftware-mansion:mainfrom
IvanIhnatsiuk:fix/mem-leak-caused-by-retain-cycle
Apr 7, 2026
Merged

fix(iOS): memory leak caused by retain cycle in layout manager and input parser#513
kacperzolkiewski merged 2 commits intosoftware-mansion:mainfrom
IvanIhnatsiuk:fix/mem-leak-caused-by-retain-cycle

Conversation

@IvanIhnatsiuk
Copy link
Copy Markdown
Contributor

Summary

The issue was caused by a strong associated reference (input) inside LayoutManagerExtension and Input parser, which retained EnrichedTextInputView. Since the input view also indirectly retains the layout manager and parser, this created a retain cycle and prevented proper deallocation.

The fix replaces the strong associated object with a weak reference using a WeakBox wrapper and __weak annotation for input in the parser. This allows ARC to correctly release EnrichedTextInputView and related objects when they are no longer needed.

Test Plan

  1. Launch Enriched input with a lot of images
  2. Observe that mem usage increased
  3. Hide(unmount) input view
  4. Observe that memory frees

Screenshots / Videos

Before:

Screen.Recording.2026-04-03.at.12.44.47.mov

After

Screen.Recording.2026-04-03.at.12.47.53.mov

Compatibility

OS Implemented
iOS
Android

Checklist

  • E2E tests are passing
  • Required E2E tests have been added (if applicable)

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a memory leak in the iOS implementation caused by retain cycles in the layout manager and input parser. The issue occurred because strong references to EnrichedTextInputView from LayoutManagerExtension and InputParser created circular references that prevented proper deallocation of these objects.

Changes:

  • Introduces a new WeakBox utility class to wrap weak references for use with Objective-C associated objects
  • Updates InputParser to use a __weak pointer instead of a strong reference for the input view
  • Updates LayoutManagerExtension to store the input reference via WeakBox to avoid the retain cycle while still maintaining the association

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
ios/utils/WeakBox.h New header defining WeakBox interface with weak property
ios/utils/WeakBox.m WeakBox implementation (auto-synthesized properties)
ios/inputParser/InputParser.mm Changed _input from strong to __weak reference
ios/extensions/LayoutManagerExtension.mm Updated to use WeakBox wrapper and corrected imports

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ios/extensions/LayoutManagerExtension.mm Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Collaborator

@kacperzolkiewski kacperzolkiewski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution!

Copy link
Copy Markdown
Collaborator

@szydlovsky szydlovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find!

@kacperzolkiewski kacperzolkiewski merged commit 1b064b8 into software-mansion:main Apr 7, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants