简体   繁体   English

Lua C API内存泄漏? (valgrind)

[英]Lua C API Memory Leak? (valgrind)

I'm trying to write a C program with Lua embedded inside.. And, I tried a very simple program to start, it just creates the Lua context, and then destroys it: 我试图编写一个嵌入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__);
    return(0);
}

I'm compiling it like so: (I'm actually using Torch7, so..) 我正在这样编译它:(我实际上在使用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

When I run it on it's own, it prints 当我自己运行它时,它会打印

pure_lua_test.C: 16

as expected, (just before the return). 如预期的那样(在返回之前)。

But, when I run it with valgrind, (as valgrind ./pure_lua_test ) 但是,当我使用valgrind运行它时(如valgrind ./pure_lua_test)

I get 我懂了

==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)

Does anyone know what's happening? 有人知道发生了什么吗? Why is it SIGSEGV'ing in valgrind? 为什么SIGSEGV在valgrind中运行? Is this something I should worry about? 这是我应该担心的事情吗? Basically, I was hoping to verify that a plugin I was writing for Torch had no memory leaks... But, if it fails with this, then, I'm kind of stuck. 基本上,我希望验证自己为Torch编写的插件没有内存泄漏...但是,如果此操作失败,那么我有点卡住了。

The reason for this issue seems to be Valgrind, not LuaJIT. 此问题的原因似乎是Valgrind,而不是LuaJIT。 Valgrind is blocking MAP_32BIT which breaks LuaJIT . Valgrind正在阻止MAP_32BIT,这会破坏LuaJIT To demonstrate, add a check for NULL on lua_State * L and you'll see it is NULL while run under Valgrind, and non- NULL while running regularly. 为了演示,在lua_State * L上添加NULL检查,您将看到在Valgrind下运行时为NULL,而在常规运行时为非NULL

Here is the modifications I did to your sample: 这是我对您的样本所做的修改:

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");
}

When I run this normally: 当我正常运行时:

$ ./pure_lua_test 
I can create luaL_newstate fine!

But when I run it under Valgrind: 但是当我在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 also report that the application exited as it should: GDB还报告该应用程序已退出,原因如下:

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

Here is a complete MCVE: 这是完整的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);
}

Here is a nice article on MAP_32BIT. 是一篇关于MAP_32BIT的不错的文章。 Hope this helps in any way. 希望这对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM