Android 处理程序的 FIND_POTENTIAL_LEAKS
Android Handler's FIND_POTENTIAL_LEAKS
在Handler.java
的源代码中,我划过下面的代码段
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());
}
}
}
从代码中,我可以看到 FIND_POTENTIAL_LEAKS
用于查找潜在 leaks.However 字段是 private
并且总是 false
。
那么什么时候才能真正用上呢?
编辑
来自 Murat,反射似乎有效,但为什么 Android
设置默认值 true
?
该字段的文档中有评论:
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;
我猜它会通过反射设置为 true
,例如此处描述 Change private static final field using Java reflection
在Handler.java
的源代码中,我划过下面的代码段
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());
}
}
}
从代码中,我可以看到 FIND_POTENTIAL_LEAKS
用于查找潜在 leaks.However 字段是 private
并且总是 false
。
那么什么时候才能真正用上呢?
编辑
来自 Murat,反射似乎有效,但为什么 Android
设置默认值 true
?
该字段的文档中有评论:
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;
我猜它会通过反射设置为 true
,例如此处描述 Change private static final field using Java reflection