fix(ios): break strong retain cycles in method-channel and custom-style callbacks#362
Open
hebaek76916 wants to merge 1 commit intonote11g:mainfrom
Open
fix(ios): break strong retain cycles in method-channel and custom-style callbacks#362hebaek76916 wants to merge 1 commit intonote11g:mainfrom
hebaek76916 wants to merge 1 commit intonote11g:mainfrom
Conversation
…le callbacks Fix a critical iOS memory leak where NaverMapView, NaverMapController, OverlayController, and the underlying NMFNaverMapView are never deallocated after a map screen is disposed. Root cause: Four separate strong retain cycles caused by passing instance methods as closure arguments. Swift automatically converts instance-method references to closures that capture `self` strongly. When stored by FlutterMethodChannel or the NMFMapView SDK, these create unbreakable retain cycles. Fixes applied: 1. NaverMapController.init — wrap setMethodCallHandler with [weak self] 2. OverlayController.init — wrap setMethodCallHandler with [weak self] 3. NaverMapControlSenderExtensions — wrap custom style callbacks 4. NaverMapViewEventDelegate — wrap setCustomStyleId callbacks 5. NaverMapView.deinit — unregister delegates + removeFromSuperview Verified with Xcode Memory Graph: all native objects are fully deallocated after each map-screen pop. Memory returns to baseline.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a critical iOS memory leak where
NaverMapView,NaverMapController,OverlayController, and the underlyingNMFNaverMapVieware never deallocated after a map screen is disposed. Apps that open/close the map repeatedly eventually hit the iOS memory high-watermark (~2 GB) and are killed by the OS.Crash signature:
Root Cause
Four separate strong retain cycles caused by passing instance methods as closure arguments. In Swift,
channel.setMethodCallHandler(handle)implicitly capturesselfstrongly, creating cycles that ARC cannot break.Cycle 1 —
NaverMapController.initchannel.setMethodCallHandler(handle)→ FlutterMethodChannel → closure → selfCycle 2 —
OverlayController.initchannel.setMethodCallHandler(handler)→ same patternCycle 3 —
NaverMapControlSenderExtensions(the real killer)loadHandler: onCustomStyleLoaded→ stored inside NMFMapView → creates NMFNaverMapView ↔ NaverMapController cycleCycle 4 —
NaverMapViewEventDelegate.registerDelegatesloadHandler: sender?.onCustomStyleLoaded→ promotes optional, captures sender stronglyFix
All four wrap callbacks in explicit
{ [weak self] in self?.method() }closures. AddeddeinittoNaverMapControllerandOverlayControllerto clear method-channel handlers. Added delegate-unregister +removeFromSuperview()inNaverMapView.deinitas safety net.Verification
Tested with Xcode Memory Graph after 4+ map-screen push/pop cycles:
Before (3 visits):
After (4 visits):
deinitmethods fire on every popChecklist
cf)


After Fix