我正在尝试编写一个嵌入了Lua的C程序..而且,我尝试了一个非常简单的程序启动,它只是创建了Lua上下文,然后将其销毁:
import random
count = 0
response = 0
yournum = 0
print("Hello! Welcome to the Lottery Picker \n ")
print("Hit the letter P to pick each line or any other letter to quit ")
response = input()
while count < 6:
#response = 0
if yournum == 0 and response == "P":
number = random.randint(1, 49)
print("Your first number is: " + str(number))
count = count + 1
yournum = yournum + 1
print("Hit the letter P to another number ")
#response = 0
response = input()
elif yournum == 1 and response == "P":
number = random.randint(1, 49)
print("Your second number is: " + str(number))
count = count + 1
yournum = yournum + 1
print("Hit the letter P to another number ")
#response = 0
response = input()
elif yournum == 2 and response == "P":
number = random.randint(1, 49)
print("Your third number is: " + str(number))
count = count + 1
yournum = yournum + 1
print("Hit the letter P to another number ")
#response = 0
response = input()
elif yournum == 3 and response == "P":
number = random.randint(1, 49)
print("Your fourth number is: " + str(number))
count = count + 1
yournum = yournum + 1
print("Hit the letter P to another number ")
#response = 0
response = input()
elif yournum == 4 and response == "P":
number = random.randint(1, 49)
print("Your fifth number is: " + str(number))
count = count + 1
yournum = yournum + 1
print("Hit the letter P to another number ")
#response = 0
response = input()
elif yournum == 5 and response == "P":
number = random.randint(1, 49)
print("And your powerball number is: " + str(number))
count = count + 1
print("Hit the letter P to another number ")
#response = 0
response = input()
else:
count = count + 1
print("Something is wrong with the lottery picker - goodbye ")
我正在编译它:(我实际上正在使用Torch7,所以..)
#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);
}
当我自己运行时,会打印
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
如预期的那样(在返回之前)。
但是,当我用valgrind运行时,(如valgrind ./pure_lua_test)
我得到了
pure_lua_test.C: 16
有谁知道发生了什么?为什么在valgrind中使用SIGSEGV?这是我应该担心的吗?基本上,我希望验证我为Torch编写的插件没有内存泄漏......但是,如果它失败了,那么,我有点卡住了。
答案 0 :(得分:2)
这个问题的原因似乎是Valgrind,而不是LuaJIT。 Valgrind是blocking MAP_32BIT which breaks LuaJIT。要进行演示,请在NULL
上添加对lua_State * L
的检查,您会在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);
}
Here是一篇关于MAP_32BIT的好文章。希望这会有所帮助。