segmentation fault when using clang_disposeIndex in a C ruby extension.

I am trying to create a litte ruby extension for the c interface of clang for fun. I have an Index class that create a CXIndex when the class is initialized.
User can set the options flags during the creation of the index like in this code:

index global options test

#=> [:None, :Threadbackgroundpriorityforindexing, :Threadbackgroundpriorityforediting, :Threadbackgroundpriorityforall]

class TestIndexGlobalOptions < MiniTest::Test
def setup
@cindex =, true)
def test_index_set_global_options_None
flags = Clangc::GlobalOptFlags::None
@cindex.global_options = flags
assert_equal flags, @cindex.global_options
def test_index_set_global_options_editing
flags = Clangc::GlobalOptFlags::Threadbackgroundpriorityforediting
@cindex.global_options = flags
assert_equal flags, @cindex.global_options

This code works, the problem is when the Garbage Collector (GC) of ruby try to clean the instances at the very end.
It generates a segmentation fault but just for the instances created with the flags Threadbackgroundpriorityforediting.

Each of my Index instance have a structure like this :

typedef struct Index_t {
CXIndex data;
} Index_t;

allocated with :

#include “class_Index.h”
#include “stdio.h”

static void
c_Index_struct_free(Index_t *s)

printf(“SENTINEL index ptr %p\n”, s->data);
printf(“SENTINEL class ptr %p\n”, s);
printf(“SENTINEL free end\n”);
static VALUE
c_Index_struct_alloc( VALUE klass)
Index_t *i;
i = (Index_t *) ruby_xmalloc(sizeof(Index_t)); //its an xmalloc
printf(“New class allocated at %p\n”, i);
i->data = NULL;
/*associate */
return Data_Wrap_Struct(klass, NULL, c_Index_struct_free,(void *) i );

The GC call c_Index_struct_free(Index_t *s) in order to clean all and the clang_disposeIndex instruction generate the segfault.

Here is the output (I have added some debugging output in my C code)

ruby test/Index_tests.rb
Run options: --seed 10378


New class allocated at 0x139bfe0
New class ptr 0x139bfe0 with Index ptr 0x1057b30
.New class allocated at 0x1350a30
New class ptr 0x1350a30 with Index ptr 0x13769f0

Finished in 0.000871s, 2297.2451 runs/s, 2297.2451 assertions/s.

2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
SENTINEL index ptr 0x1057b30
SENTINEL class ptr 0x139bfe0
SENTINEL free end
SENTINEL index ptr 0x2013769f0
test/Index_tests.rb: [BUG] Segmentation fault at 0x0003e800002782
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]

– Control frame information -----------------------------------------------
c:0001 p:0000 s:0002 E:000f60 TOP [FINISH]

– Machine register context ------------------------------------------------
RIP: 0x00007f175c9cfb9d RBP: 0x0000000000de99f0 RSP: 0x00007ffc5a05a000
RAX: 0x0000000000000000 RBX: 0x00000002013769f0 RCX: 0x000000007fffffe1
RDX: 0x00007f175fb29970 RDI: 0x00000002013769f0 RSI: 0x000000000000001f
R8: 0x00000002013769f0 R9: 0x0000000000000000 R10: 0x00007f176018e700
R11: 0x0000000000000000 R12: 0x0000000001040000 R13: 0x0000000001042648
R14: 0x000000000103e060 R15: 0x00007ffc5a05a048 EFL: 0x0000000000010206

– C level backtrace information -------------------------------------------
/usr/lib/ [0x7f175fcc2915]
/usr/lib/ [0x7f175fcc2b4c]
/usr/lib/ [0x7f175fb9cd4b]
/usr/lib/ [0x7f175fc5432e]
/usr/lib/ [0x7f175f7bd540]
/usr/lib/ [0x7f175c9cfb9d]
/home/cedlemo/.gem/ruby/2.2.0/extensions/x86_64-linux/2.2.0/clangc-0.0.1/clangc/ [0x7f175d79ff7c]
/usr/lib/ [0x7f175fbb3d21]
/usr/lib/ [0x7f175fbbd169]
/usr/lib/ [0x7f175fba3668]
/usr/lib/ [0x7f175fba38b5]
ruby [0x4008ab]
/usr/lib/ [0x7f175f7aa800]
ruby(_start+0x29) [0x4008d9]

You can see more informations here