I am trying to bind some functions from glib into Crystal. I've done 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 memory leak: the objects create with g_* functions will never be garbage collected.
Is it possible to make glib play well with Boehm GC in Crystal? Inspired by PCRE, I've tried this :
@[Link("glib-2.0")]
lib LibG
# These 2 functions work perfectly
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
# At this point happens segmentation fault
LibG.g_malloc = ->GC.malloc(LibC::SizeT)
LibG.g_free = ->GC.free(Void*)
In hope to override/redefine g_malloc
and g_free
functions.
But it does not work out: it fails with segmentation fault.
Any ideas how to make glib play with GC? I've found somehow related question, but it haven't helped me: Garbage collection with glib?
Thanks in advence.