Open thunderbiscuit opened 1 month ago
Hi @thunderbiscuit!
cargo expand
and searching for unsafe impl ::uniffi::FfiConverterArc<crate::UniFfiTag> for Amount
), you'll see this comment:/// When writing as a field of a complex structure, make a clone and transfer ownership
/// of it to the foreign-language code by writing its pointer into the buffer.
/// The foreign-language code is responsible for freeing this by calling the
/// `ffi_object_free` FFI function provided by the corresponding UniFFI type.
So your analysis is spot on: the first strong reference is the one that you created here, and the second strong reference is the one that UniFFI created for passing the Amount
to Kotlin code. Kotlin is a garbage-collected language, so while that second strong reference will be freed by the Cleaner
"eventually", there's no guarantees about when exactly that will happen.
Did you happen to try Swift, by chance? Swift's deinitializers have more predictable semantics, specifically when used with withExtendedLifetime
; I'm curious if your unwrap-or-clone optimization works there!
Arc::unwrap_or_clone
, which does exactly what you're doing now! 🎉).
We have this type (simplified for this issue):
Which we need to move to and from its Rust equivalent (which we type alias as
RustTxOut
):A dev proposed this
Arc::try_unwrap()
optimization to remove the requirement for the clone if the Arc counter is at 1. This looks good, but once I attempted to clear my understanding of the exact behaviour here through some testing, I'm left wondering if the Arc can ever be 1 and this optimization ever hit theOk
case. See my issue here for full details.The gist of it is that after testing Kotlin using the following:
And then used it in a test:
The printed output is:
So the test succeeds in printing the TxOut after consuming it in a method and we know why in this case: the reference count is 2. My question is now... why is the count at 2 here? ChatGPT seems to think it might be that uniffi holds an extra reference somewhere for you, but you know what they say about ChatGPT; NEVER TRUST THE MOFO WITH PRODUCTION CODE.
My question is then: