I am receiving an alert from Leak Canary stating that there is a leak coming from locationCallbacks. I have tried everything including making the locationCallback object itself a weak reference, which just causes a crash as it is deallocated immediately. Really need help. The Google Play Service page on GitHub has been no good: Link
I am adding my fusedLoationProvider like so:
var weakLocationCallback: WeakReference<LocationCallback>? = null
fun startLocationUpdates() {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(applicationContext)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == android.content.pm.PackageManager.PERMISSION_GRANTED) {
weakLocationCallback = WeakReference(locationCallback)
mFusedLocationClient?.requestLocationUpdates(locationRequest, weakLocationCallback!!.get(), null)
}
}
I am removing it like this:
override fun onDestroy() {
super.onDestroy()
destroyingView = true
mFusedLocationClient?.removeLocationUpdates(weakLocationCallback?.get())
weakLocationCallback?.clear()
weakLocationCallback = null
mFusedLocationClient = null
}
I am getting a leak alert every time I leave the location utilizing activity.
My locationCallback looks like this:
var locationCallback: LocationCallback? = object : LocationCallback() {
override fun onLocationResult(p0: LocationResult?) {
super.onLocationResult(p0)
val locations = p0?.locations
if (locations != null) {
val location = locations[0]
processLocation(location)
}
}
}
Here is leak canary Screenshots.