firebase-android-sdk icon indicating copy to clipboard operation
firebase-android-sdk copied to clipboard

Cannot remove ValueEventListener/ChildEventListener in onDestroy()/onStop()

Open Yaya-Dev-Box opened this issue 6 years ago • 2 comments

As the title says, I'm unable to remove the ValueEventListener/ChildEventListener, I tried with the onStop()/onDestroy() methods and tried using: lastMessagesRef.removeEventListener(lastMessagesChildListener); instead of: lastMessagesRef.orderByChild("pr").limitToLast(1).removeEventListener(lastMessagesChildListener); but the problem still persists (According to LeakCanary) The listener starts working in the onCreate() method. (This is the first time I report an issue, I understand that my report may be too brief and I'm ready to provide more infos if needed)

//  private void manageNewMessagesNotifications() {
        if (mAuth.getCurrentUser() != null) {
            lastMessagesChildListener = new ChildEventListener() {
                @Override
                public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                    if (dataSnapshot.hasChild("r")) {
                        btmNavigationView.setNotification("New", 1);
                    }
                }

                @Override
                public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                    if (dataSnapshot.hasChild("r")) {
                        btmNavigationView.setNotification("New", 1);
                    }
                }

                @Override
                public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    databaseError.toException().printStackTrace();
                }
            };
            lastMessagesRef = FirebaseDatabase.getInstance().getReference().child("LastMessages").child(mAuth.getCurrentUser().getUid());
            lastMessagesRef.orderByChild("pr").limitToLast(1).addChildEventListener(lastMessagesChildListener);
        }
    }

 @Override
    protected void onDestroy() {
        if (lastMessagesChildListener != null) {
            lastMessagesRef.orderByChild("pr").limitToLast(1).removeEventListener(lastMessagesChildListener);
        }
        super.onDestroy();
    }

    ================================================================

### LeakCanary Log

2020-02-08 21:47:05.324 564-686/? D/LeakCanary: ====================================
    HEAP ANALYSIS RESULT
    ====================================
    1 APPLICATION LEAKS
    
    References underlined with "~~~" are likely causes.
    Learn more at https://squ.re/leaks.
    
    156360 bytes retained
    ┬
    ├─ okio.AsyncTimeout
    │    Leaking: NO (PathClassLoader↓ is not leaking and a class is never leaking)
    │    GC Root: Input or output parameters in native code
    │    ↓ static AsyncTimeout.$class$classLoader
    ├─ dalvik.system.PathClassLoader
    │    Leaking: NO (Object[]↓ is not leaking and A ClassLoader is never leaking)
    │    ↓ PathClassLoader.runtimeInternalObjects
    ├─ java.lang.Object[]
    │    Leaking: NO (ZombieEventManager↓ is not leaking)
    │    ↓ array Object[].[2232]
    ├─ com.google.firebase.database.core.ZombieEventManager
    │    Leaking: NO (a class is never leaking)
    │    ↓ static ZombieEventManager.defaultInstance
    │                                ~~~~~~~~~~~~~~~
    ├─ com.google.firebase.database.core.ZombieEventManager
    │    Leaking: UNKNOWN
    │    ↓ ZombieEventManager.globalEventRegistrations
    │                         ~~~~~~~~~~~~~~~~~~~~~~~~
    ├─ java.util.HashMap
    │    Leaking: UNKNOWN
    │    ↓ HashMap.table
    │              ~~~~~
    ├─ java.util.HashMap$Node[]
    │    Leaking: UNKNOWN
    │    ↓ array HashMap$Node[].[5]
    │                           ~~~
    ├─ java.util.HashMap$Node
    │    Leaking: UNKNOWN
    │    ↓ HashMap$Node.key
    │                   ~~~
    ├─ com.google.firebase.database.core.ChildEventRegistration
    │    Leaking: UNKNOWN
    │    ↓ ChildEventRegistration.eventListener
    │                             ~~~~~~~~~~~~~
    ├─ com.firebase.ui.database.FirebaseArray
    │    Leaking: UNKNOWN
    │    ↓ FirebaseArray.mListeners
    │                    ~~~~~~~~~~
    ├─ java.util.concurrent.CopyOnWriteArrayList
    │    Leaking: UNKNOWN
    │    ↓ CopyOnWriteArrayList.elements
    │                           ~~~~~~~~
    ├─ java.lang.Object[]
    │    Leaking: UNKNOWN
    │    ↓ array Object[].[0]
    │                     ~~~
    ├─ com.thelittledev.insen.activities.ChatActivity$4
    │    Leaking: UNKNOWN
    │    Anonymous subclass of com.firebase.ui.database.FirebaseRecyclerAdapter
    │    ↓ ChatActivity$4.this$0
    │                     ~~~~~~
    ╰→ com.thelittledev.insen.activities.ChatActivity
    ​     Leaking: YES (Activity#mDestroyed is true and ObjectWatcher was watching this)
    ​     key = a1421ff9-fab8-4120-8092-75517578a4db
    ​     watchDurationMillis = 12424
    ​     retainedDurationMillis = 7418
    ====================================
    0 LIBRARY LEAKS
    
    Leaks coming from the Android Framework or Google libraries.
    ====================================
    METADATA
    
    Please include this in bug reports and Stack Overflow questions.
    
    Build.VERSION.SDK_INT: 28
    Build.MANUFACTURER: Google
    LeakCanary version: 2.0
    App process name: com.thelittledev.insen
    Analysis duration: 15812 ms
    Heap dump file path: /storage/emulated/0/Download/leakcanary-com.thelittledev.insen/2020-02-08_20-46-46_952.hprof
    Heap dump timestamp: 1581194825323
    ====================================
###

Yaya-Dev-Box avatar Feb 08 '20 20:02 Yaya-Dev-Box

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

google-oss-bot avatar Feb 08 '20 20:02 google-oss-bot

I just tested the app today and the leaks are gone, not sure what went wrong since I didn't even touch that part of the code... Guess I'll just close this issue and post a meme on reddit about how weird programming is :)

Yaya-Dev-Box avatar Feb 10 '20 20:02 Yaya-Dev-Box