Summary: Creating a persistent reference to an Activity leads to a nasty form of memory leaks (see http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html, https://corner.squareup.com/2015/05/leak-canary.html). There are many ways to create a bad persistent reference to an Activity, but the most obvious one is via a static field. This diff implements a very simple form of Activity leak checking by inspecting postconditions to see if a subtype of Activity is reachable from a static field (and it reports an error if so). This is a very simple and limited form of leak checking that does not understand the Android lifecycle at all. In particular, if one creates a persistent reference to an Activity and then nulls it out in `onDestroy` (a reasonably common pattern), this approach will wrongly report a bug.master
parent
642e6fd33a
commit
9cf74e0ce5
@ -0,0 +1,83 @@
|
||||
package codetoanalyze.java.infer;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
|
||||
public class ActivityLeaks extends Activity {
|
||||
|
||||
static Object sFld;
|
||||
|
||||
public void directLeak() {
|
||||
sFld = this;
|
||||
}
|
||||
|
||||
public void leakThenFix() {
|
||||
sFld = this;
|
||||
sFld = null;
|
||||
}
|
||||
|
||||
public void nonActivityNoLeak() {
|
||||
sFld = new Object();
|
||||
}
|
||||
|
||||
static class Obj {
|
||||
public Object f;
|
||||
}
|
||||
|
||||
public void indirectLeak() {
|
||||
Obj o = new Obj();
|
||||
o.f = this;
|
||||
sFld = o;
|
||||
}
|
||||
|
||||
public void indirectLeakThenFix() {
|
||||
Obj o = new Obj();
|
||||
o.f = this;
|
||||
sFld = o;
|
||||
o.f = null;
|
||||
}
|
||||
|
||||
class NonStaticInner {}
|
||||
|
||||
public void nonStaticInnerClassLeak() {
|
||||
sFld = new NonStaticInner();
|
||||
}
|
||||
|
||||
public void nonStaticInnerClassLeakThenFix() {
|
||||
sFld = new NonStaticInner();
|
||||
sFld = null;
|
||||
}
|
||||
|
||||
private Object o;
|
||||
|
||||
public void leakAfterInstanceFieldWrite() {
|
||||
this.o = new Object();
|
||||
sFld = this;
|
||||
}
|
||||
|
||||
public static class Singleton {
|
||||
|
||||
private static Singleton instance;
|
||||
private Context context;
|
||||
|
||||
private Singleton(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public static Singleton getInstance(Context context) {
|
||||
if(instance == null) {
|
||||
instance = new Singleton(context);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
Singleton singletonLeak() {
|
||||
return Singleton.getInstance(this);
|
||||
}
|
||||
|
||||
Singleton singletonNoLeak() {
|
||||
return Singleton.getInstance(this.getApplicationContext());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 2013 - present Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package endtoend.java.infer;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static utils.matchers.ResultContainsExactly.containsExactly;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import utils.InferException;
|
||||
import utils.InferResults;
|
||||
|
||||
public class ActivityLeaksTest {
|
||||
|
||||
public static final String SOURCE_FILE =
|
||||
"infer/tests/codetoanalyze/java/infer/ActivityLeaks.java";
|
||||
|
||||
public static final String ACTIVITY_LEAK = "ACTIVITY_LEAK";
|
||||
|
||||
private static InferResults inferResults;
|
||||
|
||||
@BeforeClass
|
||||
public static void loadResults() throws InterruptedException, IOException {
|
||||
inferResults = InferResults.loadInferResults(
|
||||
ActivityLeaksTest.class,
|
||||
SOURCE_FILE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchErrors()
|
||||
throws IOException, InterruptedException, InferException {
|
||||
String[] methods = {
|
||||
"directLeak",
|
||||
"indirectLeak",
|
||||
"nonStaticInnerClassLeak",
|
||||
"leakAfterInstanceFieldWrite",
|
||||
"singletonLeak"
|
||||
};
|
||||
assertThat(
|
||||
"Results should contain " + ACTIVITY_LEAK,
|
||||
inferResults,
|
||||
containsExactly(
|
||||
ACTIVITY_LEAK,
|
||||
SOURCE_FILE,
|
||||
methods
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue