Philip Dukhov
12/03/2020, 2:12 AMvar test: Test?
weak var testW: Test?
func f() {
testW = Test()
print(testW) // non nil, unlike Swift class instance.
DispatchQueue.main.async { [weak self] in
print(self?.testW) // now nil, kind of ok
}
}
func f() {
test = Test()
testW = test
DispatchQueue.main.async { [weak self] in
self?.test = nil
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
print(self?.testW) // not nil, seems it's not gonna be released, we have a leak
}
}
Why in the second case it not get’s release at all? It shouldn’t be captured by the block.. How can I release a kotlin object?Artyom Degtyarev [JB]
12/03/2020, 8:14 AMtest
was marked as released, the collector did not grab it yet. But it will do it at the end of execution or after some amount of time. To verify this one can either do some other operations, and check testW
after that, or call GC manually using kotlin.native.internal.GC.collect()
method(please write a wrapper to call it from Swift, it is not exposed to the framework’s API by default).
For more info, please check this and this discussion at the K/N Github.Philip Dukhov
12/03/2020, 9:07 AMArtyom Degtyarev [JB]
12/03/2020, 9:12 AMPhilip Dukhov
12/03/2020, 9:20 AM