Added track and untrack methods to TrackedDisposable's companion object so that disposables that don't extend TrackedDisposable can still take part in tracking and memory leak detection.

This commit is contained in:
Daan Vanden Bosch 2021-05-02 16:51:24 +02:00
parent b5a6ca6dc3
commit 4c37e8f741

View File

@ -9,22 +9,14 @@ abstract class TrackedDisposable : Disposable {
private set
init {
disposableCount++
if (trackPrecise) {
@Suppress("LeakingThis")
disposables.add(this)
}
@Suppress("LeakingThis")
track(this)
}
override fun dispose() {
if (!disposed) {
disposed = true
disposableCount--
if (trackPrecise) {
disposables.remove(this)
}
untrack(this)
}
}
@ -53,6 +45,22 @@ abstract class TrackedDisposable : Disposable {
}
}
fun track(disposable: Disposable) {
disposableCount++
if (trackPrecise) {
disposables.add(disposable)
}
}
fun untrack(disposable: Disposable) {
disposableCount--
if (trackPrecise) {
disposables.remove(disposable)
}
}
fun checkLeakCountZero(leakCount: Int) {
check(leakCount == 0) {
buildString {