Fix some memory leaks.

Fixes #3224
Closes #3228
// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-06-12 13:54:47 -07:00
parent e420861151
commit ed0e1c07b9
3 changed files with 18 additions and 22 deletions

View File

@ -17,7 +17,6 @@
package org.thoughtcrime.securesms.recipients; package org.thoughtcrime.securesms.recipients;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.util.Log; import android.util.Log;
@ -28,13 +27,16 @@ import org.thoughtcrime.securesms.util.FutureTaskListener;
import org.thoughtcrime.securesms.util.GroupUtil; import org.thoughtcrime.securesms.util.GroupUtil;
import org.thoughtcrime.securesms.util.ListenableFutureTask; import org.thoughtcrime.securesms.util.ListenableFutureTask;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set;
import java.util.WeakHashMap;
public class Recipient { public class Recipient {
private final static String TAG = Recipient.class.getSimpleName(); private final static String TAG = Recipient.class.getSimpleName();
private final HashSet<RecipientModifiedListener> listeners = new HashSet<>(); private final Set<RecipientModifiedListener> listeners = Collections.newSetFromMap(new WeakHashMap<RecipientModifiedListener, Boolean>());
private final long recipientId; private final long recipientId;
@ -55,7 +57,7 @@ public class Recipient {
@Override @Override
public void onSuccess(RecipientDetails result) { public void onSuccess(RecipientDetails result) {
if (result != null) { if (result != null) {
HashSet<RecipientModifiedListener> localListeners; Set<RecipientModifiedListener> localListeners;
synchronized (Recipient.this) { synchronized (Recipient.this) {
Recipient.this.name = result.name; Recipient.this.name = result.name;
@ -63,7 +65,7 @@ public class Recipient {
Recipient.this.contactUri = result.contactUri; Recipient.this.contactUri = result.contactUri;
Recipient.this.contactPhoto = result.avatar; Recipient.this.contactPhoto = result.avatar;
localListeners = (HashSet<RecipientModifiedListener>) listeners.clone(); localListeners = new HashSet<>(listeners);
listeners.clear(); listeners.clear();
} }
@ -115,18 +117,6 @@ public class Recipient {
listeners.remove(listener); listeners.remove(listener);
} }
public void notifyListeners() {
HashSet<RecipientModifiedListener> localListeners;
synchronized (this) {
localListeners = (HashSet<RecipientModifiedListener>)listeners.clone();
}
for (RecipientModifiedListener listener : localListeners) {
listener.onModified(this);
}
}
public synchronized String toShortString() { public synchronized String toShortString() {
return (name == null ? number : name); return (name == null ? number : name);
} }

View File

@ -23,9 +23,11 @@ import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.SmsMigrator; import org.thoughtcrime.securesms.database.SmsMigrator;
import org.thoughtcrime.securesms.database.SmsMigrator.ProgressDescription; import org.thoughtcrime.securesms.database.SmsMigrator.ProgressDescription;
import java.lang.ref.WeakReference;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
// FIXME: This class is nuts.
public class ApplicationMigrationService extends Service public class ApplicationMigrationService extends Service
implements SmsMigrator.SmsMigrationProgressListener implements SmsMigrator.SmsMigrationProgressListener
{ {
@ -39,9 +41,9 @@ public class ApplicationMigrationService extends Service
private final Binder binder = new ApplicationMigrationBinder(); private final Binder binder = new ApplicationMigrationBinder();
private final Executor executor = Executors.newSingleThreadExecutor(); private final Executor executor = Executors.newSingleThreadExecutor();
private Handler handler = null; private WeakReference<Handler> handler = null;
private NotificationCompat.Builder notification = null; private NotificationCompat.Builder notification = null;
private ImportState state = new ImportState(ImportState.STATE_IDLE, null); private ImportState state = new ImportState(ImportState.STATE_IDLE, null);
@Override @Override
public void onCreate() { public void onCreate() {
@ -70,7 +72,7 @@ public class ApplicationMigrationService extends Service
} }
public void setImportStateHandler(Handler handler) { public void setImportStateHandler(Handler handler) {
this.handler = handler; this.handler = new WeakReference<>(handler);
} }
private void registerCompletedReceiver() { private void registerCompletedReceiver() {
@ -103,6 +105,8 @@ public class ApplicationMigrationService extends Service
private void setState(ImportState state) { private void setState(ImportState state) {
this.state = state; this.state = state;
Handler handler = this.handler.get();
if (handler != null) { if (handler != null) {
handler.obtainMessage(state.state, state.progress).sendToTarget(); handler.obtainMessage(state.state, state.progress).sendToTarget();
} }

View File

@ -21,7 +21,6 @@ import org.thoughtcrime.securesms.jobs.GcmRefreshJob;
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory; import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFactory; import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
import org.thoughtcrime.securesms.util.DirectoryHelper; import org.thoughtcrime.securesms.util.DirectoryHelper;
import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util; import org.thoughtcrime.securesms.util.Util;
@ -34,6 +33,7 @@ import org.whispersystems.textsecure.api.TextSecureAccountManager;
import org.whispersystems.textsecure.api.push.exceptions.ExpectationFailedException; import org.whispersystems.textsecure.api.push.exceptions.ExpectationFailedException;
import java.io.IOException; import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -77,7 +77,7 @@ public class RegistrationService extends Service {
private volatile RegistrationState registrationState = new RegistrationState(RegistrationState.STATE_IDLE); private volatile RegistrationState registrationState = new RegistrationState(RegistrationState.STATE_IDLE);
private volatile Handler registrationStateHandler; private volatile WeakReference<Handler> registrationStateHandler;
private volatile ChallengeReceiver challengeReceiver; private volatile ChallengeReceiver challengeReceiver;
private String challenge; private String challenge;
private long verificationStartTime; private long verificationStartTime;
@ -298,6 +298,8 @@ public class RegistrationService extends Service {
private void setState(RegistrationState state) { private void setState(RegistrationState state) {
this.registrationState = state; this.registrationState = state;
Handler registrationStateHandler = this.registrationStateHandler.get();
if (registrationStateHandler != null) { if (registrationStateHandler != null) {
registrationStateHandler.obtainMessage(state.state, state).sendToTarget(); registrationStateHandler.obtainMessage(state.state, state).sendToTarget();
} }
@ -319,7 +321,7 @@ public class RegistrationService extends Service {
} }
public void setRegistrationStateHandler(Handler registrationStateHandler) { public void setRegistrationStateHandler(Handler registrationStateHandler) {
this.registrationStateHandler = registrationStateHandler; this.registrationStateHandler = new WeakReference<>(registrationStateHandler);
} }
public class RegistrationServiceBinder extends Binder { public class RegistrationServiceBinder extends Binder {