如何调试Lua扩展随机segfaulting

时间:2018-05-16 16:53:11

标签: c memory-leaks lua lua-api

我目前正在尝试编写一个小的Lua扩展来处理矩阵乘法。 这个工作的矩阵部分相当容易,我设法将它暴露给Lua而不需要太多努力。但是现在我面临一个奇怪的问题,我的Lua脚本随机出现了不同类型的错误。

这是概述

(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.

它似乎与内存管理和垃圾收集有关,但我已经阅读了这本书。那我怎么能找到我的错误呢?由于随机性,我似乎无法构建最小代码。除了阅读所有Lua源代码之外,我看不到我能做什么。

如果您好奇,可以查看我的库on github,它只需要阅读少量代码。

欢迎任何形式的帮助。

0 个答案:

没有答案