I'm trying to integrate the Boehm garbage collector with GLib in Linux, but in one case I have found that it is not freeing the memory: when I call g_strsplit many times, it will run out of memory and segfault. The README for the garbage collector warned that it may have trouble finding pointers inside of dynamic libraries, and may require the use of GC_add_roots.
To test this, I copied all the relevant code from GLib into my source file, not linking against libglib-2.0.so at all. This eliminated the segfaults, which tells me this is indeed the problem. However, there is no documentation on how to use GC_add_roots to resolve this. Can someone help me?
Here is the code that causes the memory leak:
#include <glib.h>
#include <gc.h>
void no_free(void *mem) {}
int main() {
g_mem_gc_friendly = TRUE;
GMemVTable memvtable = {
.malloc = GC_malloc,
.realloc = GC_realloc,
.free = no_free,
.calloc = NULL,
.try_malloc = NULL,
.try_realloc = NULL
};
g_mem_set_vtable(&memvtable);
for (int i = 0; i < 10000; i++) {
char **argv = g_strsplit("blah", " ", 0);
argv[0][0] = 'a'; // avoid unused variable warning
}
return 0;
}