To handle certain kinds of operations better (and not leak memory), NativeCall needs to be able to control the lifecycle of objects. Specifically, we need to be able to relinquish control of memory allocated on the Perl side (so that it can be handed to C code), and the ability to take responsibility for a chunk of memory (so that it will be freed when the object is GCed.
To do this, I suggest adding two new functions, adopt()
and disown()
, and reworking the string marshalling rules.
These two functions do what you'd expect. adopt()
assumes control of the memory allocated by the object, indicating that it should be freed when the object is GCed. disown()
releases control, so that the memory is untouched on GC. Neither function should apply recursively to the members of structs or arrays.
NativeCall objects created in Perl 6 will be adopted by default, and those returned from C will be disowned by default.