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