From 2a53a6677709bf0a77a798723a4aebbb92a9992a Mon Sep 17 00:00:00 2001 From: g4rb4g3 Date: Sun, 1 Dec 2024 15:26:30 +0100 Subject: [PATCH 1/4] fix: memory not freed up when component mapview is unmounted --- ios/RNMBX/RNMBXMapView.swift | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ios/RNMBX/RNMBXMapView.swift b/ios/RNMBX/RNMBXMapView.swift index 34248c843..5aa9db7d4 100644 --- a/ios/RNMBX/RNMBXMapView.swift +++ b/ios/RNMBX/RNMBXMapView.swift @@ -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 From 8e3c897964192d68a1b5f76c3adf99c45a9c627b Mon Sep 17 00:00:00 2001 From: g4rb4g3 Date: Sun, 1 Dec 2024 16:40:21 +0100 Subject: [PATCH 2/4] add (un)mount to Show Map example --- example/src/examples/Map/ShowMap.tsx | 31 +++++++++++++++++++--------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/example/src/examples/Map/ShowMap.tsx b/example/src/examples/Map/ShowMap.tsx index 3e0cea198..7c8db840d 100755 --- a/example/src/examples/Map/ShowMap.tsx +++ b/example/src/examples/Map/ShowMap.tsx @@ -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(); @@ -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); + }} /> - - + {isMounted ? ( + + - - + + + ) : null} ); }; From e6cd84eda6289d755b028f57cbdac0ac5fc02c22 Mon Sep 17 00:00:00 2001 From: g4rb4g3 Date: Tue, 7 Jan 2025 10:03:44 +0100 Subject: [PATCH 3/4] update (un-)mount example --- example/src/examples/Map/MapUnMount.tsx | 51 +++++++++++++++++++++++++ example/src/examples/Map/ShowMap.tsx | 31 +++++---------- example/src/examples/Map/index.js | 1 + 3 files changed, 62 insertions(+), 21 deletions(-) create mode 100644 example/src/examples/Map/MapUnMount.tsx diff --git a/example/src/examples/Map/MapUnMount.tsx b/example/src/examples/Map/MapUnMount.tsx new file mode 100644 index 000000000..1d4882181 --- /dev/null +++ b/example/src/examples/Map/MapUnMount.tsx @@ -0,0 +1,51 @@ +import React, { useState, useEffect } from 'react'; +import Mapbox from '@rnmapbox/maps'; +import { Button } from '@rneui/base'; + +import sheet from '../../styles/sheet'; +import { ExampleWithMetadata } from '../common/ExampleMetadata'; // exclude-from-doc + +const MapUnMount = () => { + const [isMounted, setIsMounted] = useState(true); + + useEffect(() => { + Mapbox.locationManager.start(); + + return (): void => { + Mapbox.locationManager.stop(); + }; + }, []); + + return ( + <> +