ar-g
09/23/2024, 12:24 PMdeinit
and finalize
respectively to clean up these resources when the object is deallocated.
However, in the common module of KMP, we don't have access to constructs like PhantomReference
, nor do we have destructors or finalizers that we can rely on for automatic cleanup when the object is garbage collected. Currently we support: JS(wasm), iOS, Android, JVM
TLDR: Is there an existing mechanism or recommended best practice in Kotlin Multiplatform for managing the cleanup of native resources held by objects in the common code?Oliver.O
09/23/2024, 1:11 PMHowever, in the common module of KMP, we don't have access to constructs likeWould this help? https://kotlinlang.slack.com/archives/C0BJ0GTE2/p1726162436336299orPhantomReference
WeakReference
ar-g
09/23/2024, 1:23 PMloke
09/23/2024, 1:24 PMCleaner
object?loke
09/23/2024, 1:24 PMloke
09/23/2024, 1:25 PMar-g
09/23/2024, 1:37 PMOleg Yukhnevich
09/23/2024, 1:41 PMwasm-wasi
you can take a look on code here: https://github.com/whyoleg/clozy/blob/main/clozy-core/src/commonMain/kotlin/SafeCloseable.kt
Though, I haven't had time to finalize implementation 🙂loke
09/23/2024, 1:45 PMar-g
09/23/2024, 1:46 PMOleg Yukhnevich
09/23/2024, 1:50 PMthe thing you normally want to do in the cleaner is to clean up native resourcesit's also could be used for leak-tracking. F.e if you have some closeable resource which you want to make sure that user closes (f.e TCP connection may be or some off-heap buffer, etc?) And so, if user haven't closed it, you can
close
it automatically and nudge users via logging: Hey, you haven't closed this, probably there is some bug out-there.
AFAIK Netty has leak tracker based on Cleaner
+ if we will have some kind of multiplatform FFI API - having KMP Cleaner-like API will also be usefulloke
09/23/2024, 2:06 PMloke
09/23/2024, 2:06 PM