I am currently trying to write a tiny Lua extension to handle matrix multiplication. The matrix part of the job was fairly easy and I managed to expose it to Lua without too much effort. But now I face a strange issue, my Lua script randomly segfault with different kind of error.
Here an overview
(lldb) run
Process 44058 launched: '/usr/local/bin/luad' (x86_64)
Process 44058 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x1f8)
frame #0: 0x000000010001cd19 luad`luaH_getn(t=0x00000001002024f0) at ltable.c:643
640 */
641 int luaH_getn (Table *t) {
642 unsigned int j = t->sizearray;
-> 643 if (j > 0 && ttisnil(&t->array[j - 1])) {
644 /* there is a boundary in the array part: (binary) search for it */
645 unsigned int i = 0;
646 while (j - i > 1) {
Target 0: (luad) stopped.
(lldb) run
Process 44062 launched: '/usr/local/bin/luad' (x86_64)
Process 44062 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x100503bbe)
frame #0: 0x00007fff513e8e87 libsystem_malloc.dylib`tiny_free_list_add_ptr + 108
libsystem_malloc.dylib`tiny_free_list_add_ptr:
-> 0x7fff513e8e87 <+108>: movw %r11w, -0x2(%rdx,%rax)
0x7fff513e8e8d <+114>: movw %r11w, 0x10(%rdx)
0x7fff513e8e92 <+119>: jmp 0x7fff513e8ea0 ; <+133>
0x7fff513e8e94 <+121>: testw %r11w, %r11w
Target 0: (luad) stopped.(lldb) run
Process 45023 launched: '/usr/local/bin/luad' (x86_64)
Process 45023 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
frame #0: 0x000000010000e79e luad`sweeplist(L=0x0000000101000008, p=0x00000001003059a0, count=51) at lgc.c:740
737 int white = luaC_white(g); /* current white */
738 while (*p != NULL && count-- > 0) {
739 GCObject *curr = *p;
-> 740 int marked = curr->marked;
741 if (isdeadm(ow, marked)) { /* is 'curr' dead? */
742 *p = curr->next; /* remove 'curr' from list */
743 freeobj(L, curr); /* erase 'curr' */
Target 0: (luad) stopped.
It seem to be linked to memory management and garbage collecting but I have gone by the book. So how can I find my mistake ? It seems that I cannot build a minimal code because ofrandomness. Beside reading all Lua source code I don't see what I can do.
If you are curious you can have a look at my library on github, it have a small amount of code to read.
Any kind of help is welcome.