Haxie Newb questions -- Memory Management

Hi

I am new to the Haxe community – considering using it for some cross platform apps.

I have been studying the Haxe docs and have a question around Haxe memory management.

Suppose I make a call from Haxie to an external C library (probably via some binding API) – how does Haxie:

  • free any memory the C library used to store the result of the call
  • deal with references to results allocated by the call that are subsequently stored in Haxie objects
  • deal with the fact that a garbage collector can move objects in memory (this is more of a problem with Haxie objects passed into a C call).

thanks

CW

The way this behaves (along with anything related to ffi) is usually target dependent. Usually, if you expose a C function that allocates memory, you should hopefully have a C function to free it too, so you just call that (manually).

For pointers to external objects the haxe runtime shouldn’t perform any garbage collection of the object so it shouldn’t matter if references are created. For haxe objects, this is handled the way the native garbage collector handles it, you can read more about the platform’s GC if you’re interested.

Some haxe runtimes allow you to register a pointer with the gc and add a finaliser for it for example to call a library’s free function. For example, for haxe regular expressions, the hashlink std creates a regexp object which stores a finaliser that calls pcre2_code_free: hashlink/src/std/regexp.c at 1e9ac93fb97908bbca1f7517c2261fec311d5e4b · HaxeFoundation/hashlink · GitHub

deal with the fact that a garbage collector can move objects in memory (this is more of a problem with Haxie objects passed into a C call).

This can be solved by pinning if it’s supported by the native gc, which prevents the object from being moved.