<@UFGCACPE0> there is no difference. A regular cle...
# kotlin-native
l
@CLOVIS there is no difference. A regular cleaner points to the actual object, but your other references are weak.
c
Then, how can I create a cleaner for a weak reference?
l
You don't create a cleaner for a reference. You create a cleaner for an object. When that object has no more references, the cleaner is called.
c
Do I need to keep a reference on the returned
Cleaner
?
l
Of course.
c
So I need a
WeakKeyMap<T, Cleaner>
to store all the cleaners until the object they reference has been cleaned?
l
So something like:
Copy code
class Foo {
  private val value = ...
  private val cleaner = createCleaner(value) { obj -> freeObject(obj) }
}

val weakRefToFoo = WeakReference(Foo())