Skip to content

Commit 8ffd5b6

Browse files
committed
memory leak
1 parent 069d75d commit 8ffd5b6

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

location/src/main/kotlin/io/customer/location/LocationLifecycleObserver.kt

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package io.customer.location
22

33
import androidx.lifecycle.DefaultLifecycleObserver
44
import androidx.lifecycle.LifecycleOwner
5-
import java.lang.ref.WeakReference
65

76
/**
87
* Manages location lifecycle tied to app foreground/background transitions.
@@ -12,33 +11,25 @@ import java.lang.ref.WeakReference
1211
* when [trackingMode] is [LocationTrackingMode.ON_APP_START].
1312
* - [onStop]: cancels any in-flight GPS request when the app enters background.
1413
*
15-
* Uses a [WeakReference] to [LocationServicesImpl] so that [ProcessLifecycleOwner]
16-
* (which lives for the entire process) does not prevent SDK cleanup. When the SDK
17-
* is reset via [CustomerIO.clearInstance], the services become eligible for GC,
18-
* the weak reference clears, and this observer becomes a no-op.
19-
*
2014
* Thread safety: all lifecycle callbacks are delivered on the main thread
2115
* by [ProcessLifecycleOwner], so no synchronization is needed.
2216
*/
2317
internal class LocationLifecycleObserver(
24-
locationServices: LocationServicesImpl,
18+
private val locationServices: LocationServicesImpl,
2519
private val trackingMode: LocationTrackingMode
2620
) : DefaultLifecycleObserver {
2721

28-
private val servicesRef = WeakReference(locationServices)
2922
private var hasRequestedOnStart = false
3023

3124
override fun onStart(owner: LifecycleOwner) {
32-
val services = servicesRef.get() ?: return
3325
if (trackingMode == LocationTrackingMode.ON_APP_START && !hasRequestedOnStart) {
3426
hasRequestedOnStart = true
35-
services.requestLocationUpdate()
27+
locationServices.requestLocationUpdate()
3628
}
3729
}
3830

3931
override fun onStop(owner: LifecycleOwner) {
40-
val services = servicesRef.get() ?: return
41-
val wasCancelled = services.cancelInFlightRequest()
32+
val wasCancelled = locationServices.cancelInFlightRequest()
4233
// If the GPS request was still in flight when we backgrounded,
4334
// allow onStart to retry on the next foreground entry.
4435
if (wasCancelled) {

location/src/main/kotlin/io/customer/location/LocationTracker.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.customer.sdk.core.pipeline.DataPipeline
66
import io.customer.sdk.core.pipeline.IdentifyHook
77
import io.customer.sdk.core.util.Logger
88
import io.customer.sdk.util.EventNames
9+
import java.lang.ref.WeakReference
910

1011
/**
1112
* Coordinates all location state management: persistence, restoration,
@@ -30,12 +31,18 @@ import io.customer.sdk.util.EventNames
3031
* sync filter's state.
3132
*/
3233
internal class LocationTracker(
33-
private val dataPipeline: DataPipeline?,
34+
dataPipeline: DataPipeline?,
3435
private val locationPreferenceStore: LocationPreferenceStore,
3536
private val locationSyncFilter: LocationSyncFilter,
3637
private val logger: Logger
3738
) : IdentifyHook {
3839

40+
// WeakReference prevents this hook (retained by IdentifyHookRegistry
41+
// inside a Segment plugin) from keeping CustomerIO alive after SDK
42+
// cleanup. The strong reference lives in CustomerIO.instance — as long
43+
// as the SDK is initialized, the weak ref is valid.
44+
private val dataPipelineRef = WeakReference(dataPipeline)
45+
3946
@Volatile
4047
private var lastLocation: LocationCoordinates? = null
4148

@@ -117,7 +124,7 @@ internal class LocationTracker(
117124
* track event via [DataPipeline] if both pass.
118125
*/
119126
private fun trySendLocationTrack(latitude: Double, longitude: Double) {
120-
val pipeline = dataPipeline ?: return
127+
val pipeline = dataPipelineRef.get() ?: return
121128
if (!pipeline.isUserIdentified) return
122129
if (!locationSyncFilter.filterAndRecord(latitude, longitude)) return
123130

0 commit comments

Comments
 (0)