Hello, I am trying to create a CFFI extern lib with HXCPP. (Latest git versions of hxcpp and haxe)
I have a need to return a float* created on the C side with malloc to the Haxe side, where I want to store a reference to it as a member variable of the class that uses it. This float pointer is required by a third-party C lib which I'm interfacing with (NanoVG, for manipulating transformation matrixes)
I can't work out how to link the lifetime of the Haxe class instance with the lifetime of this piece of externally malloc'd memory.
I can return the 'value' type to Haxe code by wrapping my float*
float* myMem = (float*)malloc(sizeof(float) * 6);
with
value wrappedMem = alloc_abstract(k_type, myMem);
- And this works great on the Haxe side, until the time comes when I want to kill the object holding onto this wrappedMem, and free this memory.
I try adding:
val_gc(wrappedMem, myFinalizerFn);
But myFinalizerFn runs the first time the GC runs, and the value is collected – even though I've passed a reference back into Haxe code, where the instance holding it is still alive.
I'm guessing the GC only keeps track of lifetimes of local variables there, so after the Haxe function where I assign the value to a member var has exited, it thinks it's okay to collect it.
Then I tried within Haxe code:
Gc.setFinalizer(parentObj, Function.getProcAddress("", "deallocateMem__1"));
Where parentObj is the object holding onto the value – the object whose lifetime I want to link this memory to.
It seems like the finalizer function should get called when parentObj get's GC'd - but again, it runs with the first GC collection, even though parentObj is very much alive.
(dellocateMem__1 is a CFFI function loaded with cpp.Lib.load(), as that was the only kind of reference that Gc.setFinalizer seemed to like)
Do I have some fundamental misunderstanding of how this is supposed to work?
Any help on this will be greatly appreciated!
Cheers,
Jonathan