2

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.

enter image description here

paul_f
  • 1,296
  • 17
  • 20
  • 1
    You keep creating new `WeakReference` objects. I count three just in these snippets. This implies that you have a non-weak reference to `locationCallback`, because otherwise you could not use it in your `WeakReference()` constructor calls. Since `locationCallback` is a `LocationCallback?`, try getting rid of all the `WeakReference` objects, and just setting `locationCallback = null` in `onDestroy()`. – CommonsWare Aug 03 '19 at 23:02
  • Thanks for your response, yes I know it looks a little messy but this was the last port of call. The first thing I tried was optional and set to null. Unfortunately, that doesn't work. Still leaks. – paul_f Aug 03 '19 at 23:08
  • 1
    I recommend that you roll back to that version of the code, as even with the leak, it is more stable than what you have here. In terms of help with that code... we would need to see the Leak Canary output. You might want to post screenshot(s) of what Leak Canary is telling you here. – CommonsWare Aug 03 '19 at 23:14
  • Please see screen shot attached: https://drive.google.com/open?id=1E3NuEp7ks_VlpCV126PkgHYIY8YJMTtd – paul_f Aug 04 '19 at 00:53
  • Are you sure that does not scroll any further, to show what is holding `locationCallback`? – CommonsWare Aug 04 '19 at 11:10
  • https://drive.google.com/a/hiiker.co/file/d/1GOTVx36xVxq2uIfzCOwUsOKvq4hvJdPp/view?usp=drivesdk – paul_f Aug 04 '19 at 14:13
  • Looks to me like it is the locationCallback – paul_f Aug 04 '19 at 14:14
  • Sorry, I don't have rights to view the content at that second URL. You should have enough rep to be able to edit your question and put the images directly in it, which also makes your question less reliant on your keeping these images in your Google Drive. – CommonsWare Aug 04 '19 at 14:17
  • Sorry yes, you are right. Please see in the question. – paul_f Aug 04 '19 at 14:19
  • OK. Is `TrailOverviewMap` where your `var locationCallback: LocationCallback?` resides? And, is this a `Fragment`? If the answer to both is "yes", in your `onDestroyView()`, do you have `locationCallback = null`? The one that you have in your question does not have this line, but I do not know if that code reflects what you ran that generated this Leak Canary output. – CommonsWare Aug 04 '19 at 14:27
  • No TrailOverviewMap is a Activity. Yes, I have it set to mull but this (from my reading) is an issue with locationCallback (see the link in the post). The proposed solution is generally to use weak reference but all of the sample code is using Java not Kotlin as in my project. I have tried to implement the WeakReference in a number of ways, but none of them have worked. The current state of the question is the current state of the project. I have tried numerous and creative ways to ensure that locationCallbacj is set to null but every time it leaks. – paul_f Aug 04 '19 at 14:32
  • "No TrailOverviewMap is a Activity" -- sorry, I was confusing this with another question where the asker had a fragment and `onDestroyView()`. And, it's been a bit since I dealt with Leak Canary output, so I was reading it backwards. Play Services still has a reference to your `locationCallback`. `WeakReference` will not help that, since you are not the author of Play Services, so get rid of the `WeakReference` objects. Put breakpoints on all places where you are passing `locationCallback` to Play Services and ensure that everything lines up. – CommonsWare Aug 04 '19 at 14:40

0 Answers0