Skip to content

Commit

Permalink
Work around null KeyedWeakReference.key
Browse files Browse the repository at this point in the history
- Make HahaHelper.asString() explicitly non nullable instead of crashing later with NPE
- If KeyedWeakReference.key is null (which should never happen at runtime), then we skip and keep looking for keys, but report all the null keys if we couldn't find any.

If this was caused by some malformed object then we have a chance of actually finding the right KeyedWeakReference instance. However, if this was caused by a heap dump parsing issue then we'll
still crash but at least we'll have an exception closer to the source.

Fixes #874
  • Loading branch information
pyricau committed Jul 20, 2018
1 parent 5073a1b commit b172151
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ static String valueAsString(Object value) {

/** Given a string instance from the heap dump, this returns its actual string value. */
static String asString(Object stringObject) {
checkNotNull(stringObject, "stringObject");
Instance instance = (Instance) stringObject;
List<ClassInstance.FieldValue> values = classInstanceValues(instance);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,19 @@ private String generateRootKey(RootObj root) {

private Instance findLeakingReference(String key, Snapshot snapshot) {
ClassObj refClass = snapshot.findClass(KeyedWeakReference.class.getName());
if (refClass == null) {
throw new IllegalStateException(
"Could not find the " + KeyedWeakReference.class.getName() + " class in the heap dump.");
}
List<String> keysFound = new ArrayList<>();
for (Instance instance : refClass.getInstancesList()) {
List<ClassInstance.FieldValue> values = classInstanceValues(instance);
String keyCandidate = asString(fieldValue(values, "key"));
Object keyFieldValue = fieldValue(values, "key");
if (keyFieldValue == null) {
keysFound.add(null);
continue;
}
String keyCandidate = asString(keyFieldValue);
if (keyCandidate.equals(key)) {
return fieldValue(values, "referent");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<string name="leak_canary_storage_permission_activity_label">Storage permission</string>
<string name="leak_canary_toast_heap_dump">Dumping memory, app will freeze. Brrrr.</string>
<string name="leak_canary_delete">Delete</string>
<string name="leak_canary_failure_report">"Please report this failure to http://github.com/square/leakcanary\n"</string>
<string name="leak_canary_failure_report">"Please report this failure to http://github.com/square/leakcanary and share the heapdump file that caused it.\n"</string>
<string name="leak_canary_delete_all">Delete all</string>
<string name="leak_canary_delete_all_leaks_title">Are you sure you want to delete all leaks?</string>
<string name="leak_canary_could_not_save_title">Could not save result.</string>
Expand Down

0 comments on commit b172151

Please sign in to comment.