Lua C API存在内存泄漏问题吗?(使用valgrind)

我尝试编写一个嵌入里面的Lua的C程序... 我尝试了一个非常简单的程序开始,它只是创建了Lua上下文,然后销毁它:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> extern "C" { #include <lua.h> #include <lauxlib.h> #include <lualib.h> }

int main(int argc,char * argv[]) { lua_State * L = lua_open(); luaL_openlibs(L);

lua_close(L); 
fprintf(stderr,“%s:%d\n”,__FILE__,__LINE__); 
return0); 

}


我编译它这样:(我实际上使用的是 Torch7,所以...)

g ++ -c -g3 -O2 -Wall -Werror -I/usr/local/torch/install/include/ -fPIC pure_lua_test.C -o pure_lua_test.o g ++ -g3 -O2 -Wall -Werror -I/usr/local/torch/install/include/ -fPIC -o pure_lua_test pure_lua_test.o -L/usr/local/torch/install/lib/ -lluajit


当我自己运行它时,它会打印

pure_lua_test.C: 16


预期的(在返回之前)。

但是,当我在valgrind下运行它时,(作为valgrind ./pure\_lua\_test)

我得到

== 9165 == Memcheck,一种内存错误检测器 == 9165 ==版权(C)2002-2013,由Julian Seward et al. == 9165 ==与GNU GPL的使用Valgrind-3.10.0.SVN和LibVEX;执行-h以获取版权信息 == 9165 ==命令:./pure_lua_test == 9165 == == 9165 == 4字节大小的无效读取 == 9165 == at 0x4E9EE97:lua_pushcclosure(in /usr/local/src/torch-2015-05-25/install/lib/libluajit.so) == 9165 == at 0x4EB4CDD:luaL_openlibs(in /usr/local/src/torch-2015-05-25/install/lib/libluajit.so) == 9165 == at 0x400700:main(pure_lua_test.C:13) == 9165 ==地址0x8不是堆栈'd,malloc'd或(最近)自由'd == 9165 == == 9165 == == 9165 ==进程以默认操作终止信号11(SIGSEGV) == 9165 ==访问未在映射区域内的地址0x8 == 9165 == at 0x4E9EE97:lua_pushcclosure(in /usr/local/src/torch-2015-05-25/install/lib/libluajit.so) == 9165 == at 0x4EB4CDD:luaL_openlibs(in /usr/local/src/torch-2015-05-25/install/lib/libluajit.so) == 9165 == at 0x400700:main(pure_lua_test.C:13) == 9165 ==如果您认为这是由于堆栈 == 9165 ==主线程(不太可能但 == 9165 ==可能),您可以尝试增加 == 9165 ==使用--main-stacksize=标志增加主线程堆栈大小。此运行中使用的主线程堆栈大小为8388608。 == 9165 == == 9165 == HEAP SUMMARY: == 9165 ==在退出时使用:0字节,0块 == 9165 ==总堆使用:0分配,0释放,0字节分配 == 9165 == == 9165 ==所有堆块都已释放--无泄漏可能存在 == 9165 == == 9165 ==要获取检测到的和抑制的错误计数,请重新运行:-v == 9165 == ERROR SUMMARY:来自1个上下文的1个错误(已抑制:0个)

```

有人知道发生了什么吗?为什么它在valgrind中SIGSEGV'ing?这是我应该担心的问题吗?基本上,我希望验证我为Torch编写的插件没有内存泄漏... But如果它失败,则我会陷入困境。

点赞
用户143279
用户143279

这个问题的原因似乎是 Valgrind,并不是 LuaJIT。 Valgrind 阻塞 MAP_32BIT,这会破坏 LuaJIT。为了证明这一点,对 lua_State * L 添加一个检查 NULL 的检查,你会发现在 Valgrind 下它是 NULL,而在常规运行时是非NULL

以下是我对你的示例所做的修改:

if(L == NULL) {
    printf("Could not create luaL_newstate()\n");
} else {
    luaL_openlibs(L);
    lua_close(L);
    printf("I can create luaL_newstate fine!\n");
}

当我正常运行时:

$ ./pure_lua_test
I can create luaL_newstate fine!

但运行在 Valgrind 下时:

$ valgrind ./pure_lua_test
==8211== Memcheck, a memory error detector
==8211== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==8211== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==8211== Command: ./pure_lua_test
==8211==
Could not create luaL_newstate()
==8211==

GDB 也报告应用程序正常退出:

(gdb) run
Starting program: /tmp/pure_lua_test
I can create luaL_newstate fine!
[Inferior 1 (process 8621) exited normally]

以下是完整的 MCVE:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
extern "C" {
        #include <lua.h>
        #include <lauxlib.h>
        #include <lualib.h>
}

int main(int argc, char *argv[]) {
    lua_State *L;
    L = luaL_newstate();

    if(L == NULL) {
        printf("Could not create luaL_newstate()\n");
    } else {
        luaL_openlibs(L);

        lua_close(L);
        printf("I can create luaL_newstate fine!\n");
    }

    return(0);
}

这里 是一个很好的关于 MAP_32BIT 的文章。希望这有所帮助。

2015-08-18 15:01:39