Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/unmount memory leak #3708

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions example/src/examples/Map/ShowMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ const ShowMap = () => {
data: (Mapbox.StyleURL as any)[key], // bad any, because enums
};
})
.sort(onSortOptions);
.sort(onSortOptions)
.concat({ data: 'mount', label: '(un)mount' });

const [styleURL, setStyleURL] = useState({ styleURL: _mapOptions[0].data });
const [isMounted, setIsMounted] = useState(true);

useEffect(() => {
Mapbox.locationManager.start();
Expand All @@ -42,17 +44,26 @@ const ShowMap = () => {
selectedIndex={_mapOptions.findIndex(
(i) => i.data === styleURL.styleURL,
)}
onPress={(index) => onMapChange(index, _mapOptions[index].data)}
onPress={(index) => {
const { data } = _mapOptions[index];
if (data === 'mount') {
setIsMounted((m) => !m);
return;
}
onMapChange(index, data);
}}
/>
<Mapbox.MapView
styleURL={styleURL.styleURL}
style={sheet.matchParent}
testID={'show-map'}
>
<Mapbox.Camera followZoomLevel={12} followUserLocation />
{isMounted ? (
<Mapbox.MapView
styleURL={styleURL.styleURL}
style={sheet.matchParent}
testID={'show-map'}
>
<Mapbox.Camera followZoomLevel={12} followUserLocation />

<Mapbox.UserLocation onPress={onUserMarkerPress} />
</Mapbox.MapView>
<Mapbox.UserLocation onPress={onUserMarkerPress} />
</Mapbox.MapView>
) : null}
</>
);
};
Expand Down
18 changes: 17 additions & 1 deletion ios/RNMBX/RNMBXMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,23 @@ class RNMBXCameraChanged : RNMBXEvent, RCTEvent {
}

@objc(RNMBXMapView)
open class RNMBXMapView: UIView {
open class RNMBXMapView: UIView, RCTInvalidating {

public func invalidate() {
self.removeAllFeaturesFromMap(reason: .ViewRemoval)

#if RNMBX_11
cancelables.forEach { $0.cancel() }
cancelables.removeAll()
#endif

_mapView.gestures.delegate = nil
_mapView.removeFromSuperview()
_mapView = nil

self.removeFromSuperview()
}

var imageManager: ImageManager = ImageManager()

var tapDelegate: IgnoreRNMBXMakerViewGestureDelegate? = nil
Expand Down
Loading