https://kotlinlang.org logo
Title
l

Leon Kiefer

07/29/2022, 5:12 PM
How does
AutofreeScope
and
Arena
work? do they automatically free the allocated memory when they are garbage collected, or is there some other mean to ensure native memory is freed if a kotlin object is garbage collected? We are currently wrapping a native library with a common code api and can not expose native memory management in the common code api.
l

Landry Norris

07/29/2022, 5:20 PM
Arena
has a clear method. When you
alloc
an object, the Arena adds the pointer to a linked list. When you call
clear
, it loops through this linked list and frees each pointer.
It is not safe to manually free a pointer allocated by an arena or other AutoFreeScope, other than by using methods provided by the AutoFreeScope.
Looking at the comment on the PR, it looks like the maintainer wants to use common APIs, potentially to move the tests to common code. I’m not familiar with PLMScope, but it looks like it is the same thing as memScoped, but common.
a

Arkadii Ivanov

07/29/2022, 5:34 PM
l

Leon Kiefer

07/29/2022, 5:59 PM
From the name "AutofreeScope" I would expect that the native memory is automatically freed when the holding object is garbage collected
@Landry Norris we want to use common API, so we can use the library in common code without duplicating business logic for multiple platforms
We use the
expected
class PLMScope to wrap a native placement, so we can pass it into our common api
l

Landry Norris

07/29/2022, 6:08 PM
I see
It would be hard to say for sure without seeing the PLMScope source. Scopes should work with C structs, so it shouldn’t be shareable regardless.