Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak with com.google.android.gms:play-services-ads:20.0.0 #20

Open
renezuidhof opened this issue Apr 18, 2021 · 0 comments
Open

Comments

@renezuidhof
Copy link

renezuidhof commented Apr 18, 2021

After upgrading to 20.0.0 I bumped into a memory leak after loading ads with the templates. Just sharing my findings for anyone else bumping into it.

I'm using #17 since the latest master is not working with 20.0.0.

private var nativeAd: NativeAd? = null

private fun loadAd() {
        val adLoader = AdLoader.Builder(context, getAdId())
                .forNativeAd { nativeAd: NativeAd ->
                    this.nativeAd = nativeAd

                    val styles = NativeTemplateStyle.Builder().build()

                    adTemplate.setStyles(styles)
                    adTemplate.setNativeAd(nativeAd)
                }
                .build()

        adLoader.loadAd(AdRequest.Builder().build())
    }

Fix for the memory leak:

fun onDestroyView() {
        nativeAd?.destroy()
//        adTemplate.destroyNativeAd() // I'm not using this because it's not checking for null on the NativeAd

        adTemplate.nativeAdView.destroy() // This was the part I didn't expect to be needed
        adTemplate.removeAllViews()
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant