Skip to content

Commit

Permalink
Fix bug in handling JNI roots
Browse files Browse the repository at this point in the history
* When adding support for native reference leaks (879ab7c) there was a missing bracket in an if/else branch that led to all native gc roots being ignored.

Fixes #1634
  • Loading branch information
pyricau committed Nov 25, 2019
1 parent 0911423 commit 2860a3c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 4 additions & 3 deletions shark/src/main/java/shark/internal/PathFinder.kt
Expand Up @@ -295,10 +295,11 @@ internal class PathFinder(
is HeapPrimitiveArray -> jniGlobalReferenceMatchers[objectRecord.arrayClassName]
}
if (referenceMatcher !is IgnoredReferenceMatcher) {
if (referenceMatcher is LibraryLeakReferenceMatcher)
if (referenceMatcher is LibraryLeakReferenceMatcher) {
enqueue(LibraryLeakRootNode(gcRoot.id, gcRoot, referenceMatcher))
} else {
enqueue(NormalRootNode(gcRoot.id, gcRoot))
} else {
enqueue(NormalRootNode(gcRoot.id, gcRoot))
}
}
}
else -> enqueue(NormalRootNode(gcRoot.id, gcRoot))
Expand Down
9 changes: 9 additions & 0 deletions shark/src/test/java/shark/HeapAnalyzerTest.kt
Expand Up @@ -5,6 +5,7 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import shark.GcRoot.JniGlobal
import shark.GcRoot.ThreadObject
import shark.LeakTraceElement.Type.LOCAL
import shark.LeakTraceElement.Type.STATIC_FIELD
Expand Down Expand Up @@ -146,4 +147,12 @@ class HeapAnalyzerTest {
assertThat(leak.leakTrace.elements[1].className).isEqualTo("Leaking")
}

@Test fun nativeGlobalVariableApplicationLeak() {
hprofFile.dump {
gcRoot(JniGlobal(id = "Leaking".watchedInstance {}.value, jniGlobalRefId = 42))
}

val leaks = hprofFile.checkForLeaks<HeapAnalysisSuccess>()
assertThat(leaks.applicationLeaks).hasSize(1)
}
}

0 comments on commit 2860a3c

Please sign in to comment.