@ -16,100 +16,105 @@ import android.os.Handler;
public class ContextLeaks extends Activity {
static Object sFld ;
static Object sFld ;
public void directLeak ( ) {
sFld = this ;
}
void directLeak ( ) {
sFld = this ;
}
public void leakThenFix ( ) {
sFld = this ;
sFld = null ;
}
public void leakThenFix ( ) {
sFld = this ;
sFld = null ;
}
public void nonActivityNoLeak ( ) {
sFld = new Object ( ) ;
}
public void nonActivityNoLeak ( ) {
sFld = new Object ( ) ;
}
static class Obj {
public Object f ;
}
static class Obj {
public Object f ;
}
public void indirectLeak ( ) {
Obj o = new Obj ( ) ;
o . f = this ;
sFld = o ;
}
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 ;
}
public void indirectLeakThenFix ( ) {
Obj o = new Obj ( ) ;
o . f = this ;
sFld = o ;
o . f = null ;
}
class NonStaticInner { }
class NonStaticInner {
}
public void nonStaticInnerClassLeak ( ) {
sFld = new NonStaticInner ( ) ;
}
public void nonStaticInnerClassLeak ( ) {
sFld = new NonStaticInner ( ) ;
}
public void nonStaticInnerClassLeakThenFix ( ) {
sFld = new NonStaticInner ( ) ;
sFld = null ;
}
public void nonStaticInnerClassLeakThenFix ( ) {
sFld = new NonStaticInner ( ) ;
sFld = null ;
}
private Object o ;
private Object o ;
public void leakAfterInstanceFieldWrite ( ) {
this . o = new Object ( ) ;
sFld = this ;
}
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 ( ) ) ;
}
public static class Singleton {
private Handler handler = new Handler ( ) ;
private static Singleton instance ;
private Context context ;
public void handlerLeak ( ) {
Runnable r =
new Runnable ( ) {
public void run ( ) {
}
} ;
handler . postDelayed ( r , 10000 ) ;
private Singleton ( Context context ) {
this . context = context ;
}
public void handlerNoLeak ( ) {
Runnable r =
new Runnable ( ) {
public void run ( ) {
}
} ;
handler . postDelayed ( r , 10000 ) ;
handler . removeCallbacks ( r ) ;
public static Singleton getInstance ( Context context ) {
if ( instance = = null ) {
instance = new Singleton ( context ) ;
}
return instance ;
}
}
public Singleton singletonLeak ( ) {
return Singleton . getInstance ( this ) ;
}
public Singleton singletonNoLeak ( ) {
return Singleton . getInstance ( this . getApplicationContext ( ) ) ;
}
private Handler handler = new Handler ( ) ;
public void indirectHandlerLeak ( ) {
handlerLeak ( ) ;
}
private void handlerLeak ( ) {
Runnable r =
new Runnable ( ) {
public void run ( ) {
}
} ;
handler . postDelayed ( r , 10000 ) ;
}
public void handlerNoLeak ( ) {
Runnable r =
new Runnable ( ) {
public void run ( ) {
}
} ;
handler . postDelayed ( r , 10000 ) ;
handler . removeCallbacks ( r ) ;
}
}