2015-08-12 29 views
6

Tôi đang cố gắng để viết một chương trình C với Lua nhúng bên trong .. Và, tôi đã thử một chương trình rất đơn giản để bắt đầu, nó chỉ tạo ra bối cảnh Lua, và sau đó phá hủy nó:Rò rỉ bộ nhớ API Lua C? (Valgrind)

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

tôi đang biên soạn nó như vậy: (tôi đang thực sự sử dụng Torch7, vì vậy ..)

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 

Khi tôi chạy nó trên riêng của nó, nó in

pure_lua_test.C: 16 

như mong đợi, (ngay trước khi sự trở lại).

Tuy nhiên, khi tôi chạy nó với valgrind, (như valgrind ./pure_lua_test)

tôi nhận được

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

Có ai biết những gì đang xảy ra? Tại sao nó SIGSEGV'ing trong valgrind? Đây có phải là điều tôi nên lo lắng không? Về cơ bản, tôi đã hy vọng để xác minh rằng một plugin tôi đã viết cho Torch không có rò rỉ bộ nhớ ... Nhưng, nếu nó không thành công với điều này, sau đó, tôi là loại bị mắc kẹt.

+0

Tôi đã thử thay đổi lua_open() thành luaL_newstate(), nhưng tôi vẫn gặp vấn đề tương tự với valgrind .. – bnsh

+0

Điều này có khắc phục được sự cố của bạn không? http://stackoverflow.com/questions/14840569/sigsegv-error-in-some-lua-c-code – bleakgadfly

+1

LuaJIT cần được biên dịch với sự hỗ trợ của Valgrind. Tìm kiếm LuaJIT Makefile (trong thư mục con 'src') cho' LUAJIT_USE_VALGRIND'. – siffiejoe

Trả lời

2

Lý do cho vấn đề này có vẻ là Valgrind, không phải LuaJIT. Valgrind là blocking MAP_32BIT which breaks LuaJIT. Để minh họa, hãy thêm séc cho NULL trên lua_State * L và bạn sẽ thấy nó là NULL trong khi chạy dưới Valgrind và không phải NULL trong khi chạy thường xuyên.

Dưới đây là những sửa đổi tôi đã làm để mẫu của bạn:

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

Khi tôi chạy này thường:

$ ./pure_lua_test 
I can create luaL_newstate fine! 

Nhưng khi tôi chạy nó dưới 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 cũng báo cáo rằng ứng dụng đã thoát như sau:

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

Đây là một MCVE hoàn chỉnh:

#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 là một bài viết tốt đẹp trên MAP_32BIT. Hy vọng điều này sẽ giúp bằng mọi cách.

+0

Hm. Được rồi cảm ơn! – bnsh

+0

Sự cố có vẻ đã được sửa cho phiên bản 3.11.0. – Conchylicultor

Các vấn đề liên quan