[Cuis-dev] [FFI] Passing a GC collected record to C Code

ken.dickey at whidbey.com ken.dickey at whidbey.com
Tue Jun 25 12:23:11 PDT 2019


On 2019-06-24 22:46, Philip Bernhart wrote:
> Hi Ken,
> 
> thanks for the link. One question regarding the FFI tests,
> is FFIPluginTests>>testPointers leaking memory? As the
> ffiTestPointers function mallocs a result pointer structure
> and returns the pointer to it back to the vm.

ExternalStructure>>new allocates a ByteArray which is subject to GC.

If you alloc a struct "by hand", you need to set a finalizer.  You can 
look at the Alien stuff in Squeak/Pharo to see alternate mechanics.  You 
already found ExternalAddress>>finalize.

Allocating a ByteArray on the Squeak side seems so much simpler, but you 
do need to pin the mem if you hand it to C where callbacks are possible. 
  Note ExternalData>>setHandle:type:

> And if not, how does the VM know if the pointer it receives
> can be freed / garbage collected?

Note the guard on ExternalStructure>>free:

I think you have already figured this out by now.  8^)

HTH,
-KenD


More information about the Cuis-dev mailing list