ditto-lib / live.ditto.ffi / FfiResource / retain

retain

open static fun retain(o: AutoCloseable!): Int

Provides a method for another language to express a desire to retain a Java object while in use across an FFI boundary. This is required because the Java GC has no way to know if another language is holding an opaque reference to a Java pointer. If this object has not yet been retained, a strong reference is kept and the count is initialised to 1. If the same instance has previously been retained the count will be atomically incremented. Retained objects must be AutoCloseable so they can be cleaned up predictably when the reference count later drops to zero. If the same object is also kept on the Java side it must be retained so that it does not get unexpectedly closed when the FFI finishes with it.

Parameters

o - AutoCloseable!: The resource to retain

Return
Int: The reference count after retaining