From bf017156f04ed448a0a5d113262e8981ef98d9b1 Mon Sep 17 00:00:00 2001 From: polivmi1 Date: Sun, 14 Aug 2022 14:01:57 +0200 Subject: [PATCH 1/2] #138 - prevent a memory leak on AndroidView which may not call ON_DESTROY --- .../main/java/com/google/maps/android/compose/GoogleMap.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/maps-compose/src/main/java/com/google/maps/android/compose/GoogleMap.kt b/maps-compose/src/main/java/com/google/maps/android/compose/GoogleMap.kt index de7e25f2..8fb34907 100644 --- a/maps-compose/src/main/java/com/google/maps/android/compose/GoogleMap.kt +++ b/maps-compose/src/main/java/com/google/maps/android/compose/GoogleMap.kt @@ -175,6 +175,8 @@ private fun MapLifecycle(mapView: MapView) { onDispose { lifecycle.removeObserver(mapLifecycleObserver) context.unregisterComponentCallbacks(callbacks) + mapView.onDestroy() + mapView.removeAllViews() } } } @@ -195,7 +197,9 @@ private fun MapView.lifecycleObserver(previousState: MutableState this.onResume() Lifecycle.Event.ON_PAUSE -> this.onPause() Lifecycle.Event.ON_STOP -> this.onStop() - Lifecycle.Event.ON_DESTROY -> this.onDestroy() + Lifecycle.Event.ON_DESTROY -> { + //handled in onDispose + } else -> throw IllegalStateException() } previousState.value = event From ad0b9a67406b3413748b27e4a89fc80126f71989 Mon Sep 17 00:00:00 2001 From: polivmi1 Date: Sun, 14 Aug 2022 14:01:57 +0200 Subject: [PATCH 2/2] fix: Prevent a memory leak on AndroidView which may not call ON_DESTROY --- .../main/java/com/google/maps/android/compose/GoogleMap.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/maps-compose/src/main/java/com/google/maps/android/compose/GoogleMap.kt b/maps-compose/src/main/java/com/google/maps/android/compose/GoogleMap.kt index de7e25f2..8fb34907 100644 --- a/maps-compose/src/main/java/com/google/maps/android/compose/GoogleMap.kt +++ b/maps-compose/src/main/java/com/google/maps/android/compose/GoogleMap.kt @@ -175,6 +175,8 @@ private fun MapLifecycle(mapView: MapView) { onDispose { lifecycle.removeObserver(mapLifecycleObserver) context.unregisterComponentCallbacks(callbacks) + mapView.onDestroy() + mapView.removeAllViews() } } } @@ -195,7 +197,9 @@ private fun MapView.lifecycleObserver(previousState: MutableState this.onResume() Lifecycle.Event.ON_PAUSE -> this.onPause() Lifecycle.Event.ON_STOP -> this.onStop() - Lifecycle.Event.ON_DESTROY -> this.onDestroy() + Lifecycle.Event.ON_DESTROY -> { + //handled in onDispose + } else -> throw IllegalStateException() } previousState.value = event