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;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
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.ListenableFutureTask;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.WeakHashMap;
public class Recipient {
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;
@ -55,7 +57,7 @@ public class Recipient {
@Override
public void onSuccess(RecipientDetails result) {
if (result != null) {
HashSet<RecipientModifiedListener> localListeners;
Set<RecipientModifiedListener> localListeners;
synchronized (Recipient.this) {
Recipient.this.name = result.name;
@ -63,7 +65,7 @@ public class Recipient {
Recipient.this.contactUri = result.contactUri;
Recipient.this.contactPhoto = result.avatar;
localListeners = (HashSet<RecipientModifiedListener>) listeners.clone();
localListeners = new HashSet<>(listeners);
listeners.clear();
}
@ -115,18 +117,6 @@ public class Recipient {
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() {
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.ProgressDescription;
import java.lang.ref.WeakReference;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
// FIXME: This class is nuts.
public class ApplicationMigrationService extends Service
implements SmsMigrator.SmsMigrationProgressListener
{
@ -39,7 +41,7 @@ public class ApplicationMigrationService extends Service
private final Binder binder = new ApplicationMigrationBinder();
private final Executor executor = Executors.newSingleThreadExecutor();
private Handler handler = null;
private WeakReference<Handler> handler = null;
private NotificationCompat.Builder notification = null;
private ImportState state = new ImportState(ImportState.STATE_IDLE, null);
@ -70,7 +72,7 @@ public class ApplicationMigrationService extends Service
}
public void setImportStateHandler(Handler handler) {
this.handler = handler;
this.handler = new WeakReference<>(handler);
}
private void registerCompletedReceiver() {
@ -103,6 +105,8 @@ public class ApplicationMigrationService extends Service
private void setState(ImportState state) {
this.state = state;
Handler handler = this.handler.get();
if (handler != null) {
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.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
import org.thoughtcrime.securesms.util.DirectoryHelper;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
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 java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.concurrent.ExecutorService;
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 Handler registrationStateHandler;
private volatile WeakReference<Handler> registrationStateHandler;
private volatile ChallengeReceiver challengeReceiver;
private String challenge;
private long verificationStartTime;
@ -298,6 +298,8 @@ public class RegistrationService extends Service {
private void setState(RegistrationState state) {
this.registrationState = state;
Handler registrationStateHandler = this.registrationStateHandler.get();
if (registrationStateHandler != null) {
registrationStateHandler.obtainMessage(state.state, state).sendToTarget();
}
@ -319,7 +321,7 @@ public class RegistrationService extends Service {
}
public void setRegistrationStateHandler(Handler registrationStateHandler) {
this.registrationStateHandler = registrationStateHandler;
this.registrationStateHandler = new WeakReference<>(registrationStateHandler);
}
public class RegistrationServiceBinder extends Binder {