I am trying to link some functions from glib to Crystal. I did this and it works:
@[Link("glib-2.0")]
lib LibG
fun g_utf8_strup(str : UInt8*, len : UInt32) : UInt8*
fun g_utf8_strdown(str : UInt8*, len : UInt32) : UInt8*
end
However, it introduces a memory leak: objects created using the g_ * functions will never be garbage collected.
Is it possible for glib to play well with GC Boehm in Crystal? Inspired by PCRE , I tried this:
@[Link("glib-2.0")]
lib LibG
fun g_utf8_strup(str : UInt8*, len : UInt32) : UInt8*
fun g_utf8_strdown(str : UInt8*, len : UInt32) : UInt8*
alias Malloc = LibC::SizeT -> Void*
alias Free = Void* ->
$g_malloc : Malloc
$g_free : Free
end
LibG.g_malloc = ->GC.malloc(LibC::SizeT)
LibG.g_free = ->GC.free(Void*)
Hoping to override / override the functions g_malloc
and g_free
. But this will not work: it fails with a segmentation error.
Any ideas how to get glib to play with GC? I found some related question, but that didn't help me: Garbage collection with glib?
Thanks.