On 14/09/2015 22:36, Phil Clayton wrote:
I am trying to create an SML binding to a C function that returns an array that the caller must free. Usually, there is a free function that takes just the array (as a pointer) which can be attached as a finalizer with CInterface.setFinal. I have encountered a case [1] where the caller must also pass the size of the array, returned when the array is created, to the free function.
Phil, I wonder if this is a case for the use of a weak reference ( http://www.polyml.org/documentation/Reference/Weak.html ) ? Have a look at this link and see whether it would work for you.
Essentially, what weak references do is allow you to run finalisers in "ML space". CInterface.setFinal sets up a C function to be run as a finaliser and the GC runs these immediately when it detects an unreferenced "vol". It can't run an ML function at that point because the ML heap is still being collected. Changes to weak references are notified at some point after ML is resumed after the GC.
If you've got any questions please ask. I'd also be interested to know if it solves your problem.
Regards, David