Search code examples
androidfirebasefirebase-authenticationleakcanary

Leak at zza.zza FirebaseAuthFallbackService


The app works perfectly but in LeakCanary I get a leak every time I close the app or pause it.

I also tried to delete authListener but still the leak remains.

I have tried to solve in many different ways but without success, can anyone help me?

My implementations are:

implementation platform('com.google.firebase:firebase-bom:28.2.0')    
implementation 'com.google.firebase:firebase-auth-ktx'
implementation 'com.firebaseui:firebase-ui-auth:7.2.0'

My cutted code is:

class MainActivity : AppCompatActivity() {

private lateinit var auth: FirebaseAuth
private lateinit var authListener: AuthStateListener

private val signInLauncher = registerForActivityResult(
    FirebaseAuthUIActivityResultContract()
) { res -> this.onSignInResult(res) }

override fun onCreate(savedInstanceState: Bundle?) {

    auth = Firebase.auth
    authListener = checkCurrentUser()
}

public override fun onStart() {
    super.onStart()
    auth.addAuthStateListener(authListener)
}

private fun checkCurrentUser() : AuthStateListener {

    return AuthStateListener {
        val user = it.currentUser

        if (user != null && !user.isAnonymous()) {
            // User is signed in

        } else {
            // No user is signed in
            signInAnonymously()
        }
    }
}

private fun signInAnonymously() {
    auth.signInAnonymously()
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                Log.d(AnonymousTAG, "signInAnonymously:success")
                //val user = auth.currentUser
            } else {
                // If sign in fails, display a message to the user.
                Log.w(AnonymousTAG, "signInAnonymously:failure", task.exception)
                
            }
        }
}

override fun onStop() {
    auth.removeAuthStateListener(authListener)
    super.onStop()
}

override fun onDestroy() {
    AppDatabase.distruggiInstance()
    super.onDestroy()
}

}

Whenever I close the app or pause it, I get this leak:

┬───
│ GC Root: Global variable in native code
│
├─ com.google.firebase.auth.api.fallback.service.zza instance
│    Leaking: UNKNOWN
│    Retaining 1,9 kB in 10 objects
│    zza instance of com.google.firebase.auth.api.fallback.service.
│    FirebaseAuthFallbackService
│    ↓ zza.zza
│          ~~~
╰→ com.google.firebase.auth.api.fallback.service.FirebaseAuthFallbackService
​  instance
​     Leaking: YES (ObjectWatcher was watching this because com.google.firebase.
​     auth.api.fallback.service.FirebaseAuthFallbackService received
​     Service#onDestroy() callback and Service not held by ActivityThread)
​     Retaining 1,3 kB in 9 objects
​     key = b2e73ab0-d520-4fb8-b858-c9c7a9012f2d
​     watchDurationMillis = 5205
​     retainedDurationMillis = 202
​     mApplication instance of androidx.multidex.MultiDexApplication
​     mBase instance of android.app.ContextImpl

METADATA

Build.VERSION.SDK_INT: 30
Build.MANUFACTURER: Xiaomi
LeakCanary version: 2.7
App process name: it.xxxx.yyyyyyyyyy
Stats: LruCache[maxSize=3000,hits=2287,misses=64227,hitRate=3%]
RandomAccess[bytes=3141160,reads=64227,travel=29029887588,range=22210809,size=28
401420]
Heap dump reason: 1 retained objects, app is not visible
Analysis duration: 13350 ms```

Solution

  • Edit: 17/03/2022

    As mentioned in the Github issue link (link posted below), this has been fixed in Firebase Authentication Library version 21.0.2.


    The Firebase Android client SDK is causing a memory leak and many users are facing it.

    This is filed as an internal bug on the Github repository firebase/firebase-android-sdk issue #2387.