[Android] Use WeakReference in MediaLibrary to prevent leaks

Geoffrey Métais git at videolan.org
Wed May 13 12:16:12 CEST 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed May 13 11:11:53 2015 +0200| [5e53e73c5fec556c7226ef7b09d53bf98b492d44] | committer: Geoffrey Métais

Use WeakReference in MediaLibrary to prevent leaks

In case Activity/fragment lifecycle doesn't occur correctly, a fragment reference can be retained

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=5e53e73c5fec556c7226ef7b09d53bf98b492d44
---

 vlc-android/src/org/videolan/vlc/MediaLibrary.java |   24 ++++++++++++--------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaLibrary.java b/vlc-android/src/org/videolan/vlc/MediaLibrary.java
index e20d374..6fde980 100644
--- a/vlc-android/src/org/videolan/vlc/MediaLibrary.java
+++ b/vlc-android/src/org/videolan/vlc/MediaLibrary.java
@@ -41,6 +41,7 @@ import java.io.File;
 import java.io.FileFilter;
 import java.io.IOException;
 import java.lang.Thread.State;
+import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -64,7 +65,7 @@ public class MediaLibrary {
     private boolean isStopping = false;
     private boolean mRestart = false;
     protected Thread mLoadingThread;
-    private IBrowser mBrowser = null;
+    private WeakReference<IBrowser> mBrowser = null;
 
     public final static HashSet<String> FOLDER_BLACKLIST;
     static {
@@ -248,8 +249,8 @@ public class MediaLibrary {
             final MediaDatabase mediaDatabase = MediaDatabase.getInstance();
 
             // show progressbar in footer
-            if (mBrowser != null)
-                mBrowser.showProgressBar();
+            if (mBrowser != null && mBrowser.get() != null)
+                mBrowser.get().showProgressBar();
 
             List<File> mediaDirs = mediaDatabase.getMediaDirs();
             if (mediaDirs.size() == 0) {
@@ -354,9 +355,9 @@ public class MediaLibrary {
                 // Process the stacked items
                 for (File file : mediaToScan) {
                     String fileURI = LibVLC.PathToURI(file.getPath());
-                    if (mBrowser != null)
-                        mBrowser.sendTextInfo(file.getName(), count,
-                            mediaToScan.size());
+                    if (mBrowser != null && mBrowser.get() != null)
+                        mBrowser.get().sendTextInfo(file.getName(), count,
+                                mediaToScan.size());
                     count++;
                     if (existingMedias.containsKey(fileURI)) {
                         /**
@@ -417,9 +418,9 @@ public class MediaLibrary {
                 }
 
                 // hide progressbar in footer
-                if (mBrowser != null) {
-                    mBrowser.clearTextInfo();
-                    mBrowser.hideProgressBar();
+                if (mBrowser != null && mBrowser.get() != null) {
+                    mBrowser.get().clearTextInfo();
+                    mBrowser.get().hideProgressBar();
                 }
 
                 Util.actionScanStop();
@@ -475,6 +476,9 @@ public class MediaLibrary {
     }
 
     public void setBrowser(IBrowser browser) {
-        mBrowser = browser;
+        if (browser != null)
+            mBrowser = new WeakReference<IBrowser>(browser);
+        else
+            mBrowser.clear();
     }
 }



More information about the Android mailing list