googlecast / CastVideos-android

Reference Android Sender w/ Framework API: CastVideos-android application shows how to cast videos from an Android device that is fully compliant with the Cast Design Checklist.
Apache License 2.0
345 stars 183 forks source link

[Sender] CastContext leaking reference to activity in 19.0.0 #99

Closed Ethan1983 closed 1 year ago

Ethan1983 commented 4 years ago

Hi Team,

CastContext has a static member zzkf which in some cases ends up holding reference to activity (depending upon context type). When getSharedInstance is invoked from activity (var0), it is used to get a MediaRouter instance and this in turn is kept track in zzkf. This has changed in 19.0.0.

public static CastContext getSharedInstance(@NonNull Context var0) throws IllegalStateException {
        Preconditions.checkMainThread("Must be called from the main thread.");
        if (zzkf == null) {
            synchronized(zzkg) {
                if (zzkf == null) {
                    OptionsProvider var2 = zzc(var0.getApplicationContext());

                    try {
                        zzkf = new CastContext(var0, var2.getCastOptions(var0.getApplicationContext()), var2.getAdditionalSessionProviders(var0.getApplicationContext()), new zzav(MediaRouter.getInstance(var0)));
                    } catch (zzad var5) {
                        throw new RuntimeException(var5);
                    }
                }
            }
        }

        return zzkf;
    }
Ethan1983 commented 4 years ago

@anjaneesh Could you have a look at this? This is not a Demo issue per se instead is on sender SDK.

Ethan1983 commented 4 years ago

Confirmed leak via Leak Canary in the demo app too.

Screenshot_20201117-161523

Pitel commented 2 years ago

Also happening in 21.0.1.

Ethan1983 commented 2 years ago

Work around was just to use application context.

strangesource commented 1 year ago

I know this is an old issue but I stumbled upon this when debugging a memory leak and it helped me to pinpoint the problem. The issue seems to be fixed with Android Sender 21.1.0 (ref).

Ethan1983 commented 1 year ago

Yeah, it's been fixed.