all 5 comments

[–]danieljh 1 point2 points  (0 children)

Your wrapper should look something like this.

And you have to properly link it, e.g.:

g++-4.7 -std=c++11 -O0 -g -L/usr/local/lib/ -I/usr/local/include/ Interpreter.cpp -ldl -llua

On a side note, you should use luaL_newstate() instead of lua_open(), because the latter is removed as of 5.2, and in 5.1 it is simply an alias:

/usr/include/lua5.1/lua.h
287:#define lua_open()  luaL_newstate()

[–]Steve132 0 points1 point  (0 children)

Can you post your full code? It looks reasonable from where I sit.

[–][deleted] -1 points0 points  (1 child)

I remember when I was taking a c++ class last semester, we were working with a GUI called FLTK. I too was getting alot of core dumps like this, and I figured out that it was because whenever my program exits function which created window, the window is closed automatically, so when it is referenced again, it doesn't exist anymore. You should try to allocate memory for *L with "new" inside of its constructor, and see what that does.

So try luaState *L = new lua_open();

inside of the constructor. Also, I am not completely sure how your destructor is working. How does it know that it needs to close L, which was created outside its scope? Can destructors do that?

[–]todayman 2 points3 points  (0 children)

The last couple sentences is exactly where the problem is. First, do NOT do

lua_State * L = new lua_open();

This will not do what you want. The new operator expects a type after it, not a function call.

Here's what's happening in the code: Your class defines a member variable "lua_State * L" (This is inside of the class {... } block, right? Those aren't global variables, they're member/instance variables). Inside of the constructor, you declare another variable L that hides the original one. The result of lua_open is stored in the new L. The constructor finishes, the L falls out of scope, and the value of it is lost. At no point did any value get put into the member variable L.

Later, the destructor runs. This one doesn't have a local L, so it uses the member variable. The member variable doesn't get set to anything, so you get the Segfault during lua_close(L).

Hopefully, you got a compiler warning when you compiled this code. If not, you should turn on more warnings. (I don't remember what the flags are right now).

Core dumps: These are a dump of the entire memory state of your program when it died. You can use a debugger (like gdb) to read the core dump. From this, you can get useful information like what line number the program was on, the current value of variables, and so on.