Lua C API内存泄漏?(valgrind)
Lua C API Memory Leak? (valgrind)
我想写一个C程序与Lua内嵌…然后,我尝试了一个非常简单的程序来启动,它只是创建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: %dn", __FILE__, __LINE__);
return(0);
}
我像这样编译它:(我实际上使用的是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, a memory error detector
==9165== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==9165== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==9165== Command: ./pure_lua_test
==9165==
==9165== Invalid read of size 4
==9165== at 0x4E9EE97: lua_pushcclosure (in /usr/local/src/torch-2015-05-25/install/lib/libluajit.so)
==9165== by 0x4EB4CDD: luaL_openlibs (in /usr/local/src/torch-2015-05-25/install/lib/libluajit.so)
==9165== by 0x400700: main (pure_lua_test.C:13)
==9165== Address 0x8 is not stack'd, malloc'd or (recently) free'd
==9165==
==9165==
==9165== Process terminating with default action of signal 11 (SIGSEGV)
==9165== Access not within mapped region at address 0x8
==9165== at 0x4E9EE97: lua_pushcclosure (in /usr/local/src/torch-2015-05-25/install/lib/libluajit.so)
==9165== by 0x4EB4CDD: luaL_openlibs (in /usr/local/src/torch-2015-05-25/install/lib/libluajit.so)
==9165== by 0x400700: main (pure_lua_test.C:13)
==9165== If you believe this happened as a result of a stack
==9165== overflow in your program's main thread (unlikely but
==9165== possible), you can try to increase the size of the
==9165== main thread stack using the --main-stacksize= flag.
==9165== The main thread stack size used in this run was 8388608.
==9165==
==9165== HEAP SUMMARY:
==9165== in use at exit: 0 bytes in 0 blocks
==9165== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==9165==
==9165== All heap blocks were freed -- no leaks are possible
==9165==
==9165== For counts of detected and suppressed errors, rerun with: -v
==9165== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
有人知道发生了什么事吗?为什么在valgrind中使用SIGSEGV ?这是我该担心的吗?基本上,我希望验证我为Torch编写的插件没有内存泄漏……但是,如果这个失败了,那么,我就卡住了。
这个问题的原因似乎是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的好文章。
相关文章:
- valgrind-hellgrind与泄漏检查的结果不同
- Valgrind 在 QThread::start() 上报告内存泄漏
- 使用 valgrind 检查我的链表暗示中的内存泄漏,让我"肯定丢失:1 个块中有 40 个字节"
- Valgrind 在 std::make_unique 中显示内存泄漏
- 使用Valgrind时内存泄漏消失
- 如何通过Valgrind找到QThreads程序中的内存泄漏在哪里?
- C++中的动态数组:Valgrind泄漏
- 为什么 Valgrind 在此实现中报告内存泄漏?
- 为什么使用 make_unique<> 将函数传递给线程会导致 Valgrind 中的内存泄漏?
- 客户端代码中的 Valgrind 钩子:特定函数的内存泄漏
- Valgrind 错误和内存泄漏与 Python/C API
- 如何修复 poco Poco::Net::TCPServerParams() valgrind 明确的泄漏
- 如何使用 Valgrind 确定内存泄漏"still-available"来源?
- Valgrind检测到OpenGL内存泄漏
- Valgrind和Qemu-无法检测到内存泄漏
- Tensorflow C 内存泄漏 - valgrind
- 如何修复Valgrind报告的C 数组驱动器中的内存泄漏
- 带有Valgrind的C 的向量分配中的内存泄漏
- 用valgrind分析c++字符内存泄漏
- Valgrind 在主机名命令输出中显示内存泄漏