Sharas
Sharas

Reputation: 2135

LeakCanary - Leak on destroyView - what does it mean?

I'm using leakCanary in my debug app and every time my fragment is destroyed, I'm getting a leak warning. the leak tree starts with saying that there's no leak in the Fragment, only this part is leaking, and it's on every fragment.

What does this leak means? is there a way to avoid it?

androidx.constraintlayout.widget.ConstraintLayout instance
    ​     Leaking: YES (ObjectWatcher was watching this because MyFragment received
    ​     Fragment#onDestroyView() callback (references to its views should be
    ​     cleared to prevent leaks))
    ​     Retaining 13.0 kB in 124 objects
    ​     key = 0b4d582b-b8ac-42dc-aebf-7b7b0804c92e
    ​     watchDurationMillis = 127274
    ​     retainedDurationMillis = 122272
    ​     View not part of a window view hierarchy
    ​     View.mAttachInfo is null (view detached)
    ​     View.mWindowAttachCount = 1
    ​     mContext instance of via.driver.v2.map.MapActivityV2 with mDestroyed =
    ​     false

Upvotes: 3

Views: 2266

Answers (1)

Stixe
Stixe

Reputation: 1

You should always clear all references to views on fragments onDestroyView or onDestroy methods because if you don't, it's a guaranteed memory leak. This looks like a google map leak, as far as I know it has always had this problem.

Try using the code below and it should no longer happen:

@Override
protected void onDestroy() {
    super.onDestroy();
    mMap.clear();
    mapView.onDestroy();
    mapView.removeAllViews();
}

Upvotes: 0

Related Questions