Glib binding in Crystal lang (GC issue)

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
  # 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*)

Hoping to override / override the functions g_mallocand 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.

+4
1

gobject-introspection . .GIR , XML , API , , , . , GLib.

, , .

, , g_malloc g_free. , gobject-introspection JavaScript, , JS . , , C; , JS ( ), . , JS, . JS JS.

GLib - , , - JS ; JS GC'd, , C , JS.

+1

Source: https://habr.com/ru/post/1628039/


All Articles