Why does the `init` block in this case warn: `Leak...
# getting-started
m
Why does the
init
block in this case warn:
Leaking 'this' in constructor of non-final class A
but not in the
.apply
case? Is the receiver block guaranteed to run after construction of
A
completes somehow, or is this just a case that isn’t caught by the linter?
Copy code
abstract class A(injectedB: B) {
    init {
        injectedB.observeA(this)
    }
    
    val constructedB = B().apply {
        observeA(this@A)
    }
}

class B {
    fun observeA(a: A) {}
}
t
Probably not caught by the linter
m
well you can try to reference the
a
in the
observe
method and you will know whether you got NPE or not 🙂
137 Views