Are there any plans to add destructors to Kotlin/n...
# kotlin-native
s
Are there any plans to add destructors to Kotlin/native? I'm aware there is a stack overflow answer on this, but it's a year old, and things change.
o
we do not see enough motivation to change something here, and with weak references some usecases of destructors like figuring out if some resource no longer needed would be handled as well
n
Can weak references be used with a GUI toolkit/lib (eg GTK) for event handling?
o
what is the point of doing that?
t
If the event handlers close over references to GUI objects and those objects also have references to the event handlers (because they're attached to events), there is a cycle that won't be collected with pure reference counting. This used to be a big problem in JS on Internet Explorer 7 and earlier, where the DOM was based on reference-counted COM objects. So it would be nice to have the GC handle the wrapping Kotlin objects and have the destructors destroy the wrapped native objects. But maybe there is a better way, and I have my head stuck in the 90s.
o
Kotlin/Native can collect cyclic garbage, do you have an example where this problem actually shows up?
t
Hmm... I guess the kind of reference cycle I was thinking of, between Kotlin and native, isn't possible because staticCFunction can't capture any values.
o
It depends on how cycle appears, but if it goes via native stable pointer - you ‘re right
n
On the JVM side for example this is a common problem (as Henrik has mentioned earlier but in a different environment) when using JavaFX or Swing with a JVM language (eg Kotlin), hence weak references are used.