Skip to content

MediaNotificationManager retains service after onDestroy is called #2692

@derat

Description

@derat

Leak Canary reports that my service is still retained by MediaNotificationManager after the service's onDestroy method has been called:

┬───
│ GC Root: System class
│
├─ android.app.ActivityThread class
│    Leaking: NO (MessageQueue↓ is not leaking and a class is never leaking)
│    ↓ static ActivityThread.sMainThreadHandler
├─ android.app.ActivityThread$H instance
│    Leaking: NO (MessageQueue↓ is not leaking)
│    ↓ Handler.mQueue
├─ android.os.MessageQueue instance
│    Leaking: NO (MessageQueue#mQuitting is false)
│    HandlerThread: "main"
│    ↓ MessageQueue[6]
│                  ~~~
├─ android.os.Message instance
│    Leaking: UNKNOWN
│    Retaining 15.2 MB in 69260 objects
│    Message.what = 1
│    Message.when = 1391158535 (577265 ms after heap dump)
│    Message.obj = null
│    Message.callback = null
│    Message.target = instance @337816704 of android.os.Handler
│    ↓ Message.target
│              ~~~~~~
├─ android.os.Handler instance
│    Leaking: UNKNOWN
│    Retaining 15.2 MB in 69259 objects
│    ↓ Handler.mCallback
│              ~~~~~~~~~
├─ androidx.media3.session.MediaNotificationManager instance
│    Leaking: UNKNOWN
│    Retaining 15.2 MB in 69258 objects
│    mediaSessionService instance of org.erat.nup.NupService
│    ↓ MediaNotificationManager.mediaSessionService
│                               ~~~~~~~~~~~~~~~~~~~
╰→ org.erat.nup.NupService instance
​     Leaking: YES (ObjectWatcher was watching this because org.erat.nup.
​     NupService received Service#onDestroy() callback and Service not held by
​     ActivityThread)
​     Retaining 1.8 MB in 54083 objects
​     key = 116a380f-a4a8-4ca4-a8d1-8784a085613c
​     watchDurationMillis = 14100
​     retainedDurationMillis = 9092
​     windowContext instance of android.window.WindowContext
​     mApplication instance of org.erat.nup.NupApplication
​     mBase instance of android.app.ContextImpl

My service is derived from MediaLibraryService. I create a MediaLibrarySession in my service's onCreate method:

mediaSession = MediaLibrarySession.Builder(this, core.playerImpl, mediaHelper)
    .setSessionActivity(
         PendingIntent.getActivity(
            this,
            0,
            Intent(this, NupActivity::class.java),
            PendingIntent.FLAG_IMMUTABLE,
        ),
    ).build()

I call the session's release method in onDestroy:

mediaSession.release()

In my activity, I'm explicitly stopping my service in response to a user action:

stopService(Intent(this, NupService::class.java))
finishAndRemoveTask()

Is it expected that MediaNotificationManager will remain active and retain my service in this case?

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions