https://kotlinlang.org logo
#getting-started
Title
# getting-started
m

matt tighe

07/27/2020, 7:47 PM
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

tseisel

07/27/2020, 8:55 PM
Probably not caught by the linter
m

Milan Hruban

07/28/2020, 7:37 AM
well you can try to reference the
a
in the
observe
method and you will know whether you got NPE or not 🙂
108 Views