Search code examples
androidandroid-handler

Android Handler's FIND_POTENTIAL_LEAKS


In the source code of Handler.java,I cross below code segment

public Handler(Callback callback, boolean async) {
    if (FIND_POTENTIAL_LEAKS) {
        final Class<? extends Handler> klass = getClass();
        if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
                (klass.getModifiers() & Modifier.STATIC) == 0) {
            Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
                klass.getCanonicalName());
        }
    }

}

From code,I can see FIND_POTENTIAL_LEAKS is used to find potential leaks.However the filed is private and always false.

So when it will be really made use of?

EDIT

From Murat, reflection seems to work but why Android set the value default true ?


Solution

  • There is a comment in the docs for that field:

     public class Handler {
    65     /*
    66      * Set this flag to true to detect anonymous, local or member classes
    67      * that extend this Handler class and that are not static. These kind
    68      * of classes can potentially create leaks.
    69      */
    70     private static final boolean FIND_POTENTIAL_LEAKS = false;
    

    I guess it's getting set to true via reflections e.g. described here Change private static final field using Java reflection