Lua with C++: Program crashes when lua_close() called

Started by
1 comment, last by Katie 11 years, 5 months ago
After a bit of a struggle, I've gotten Lua to work with Visual Studio 2012 (Lua 5.2). Unfortunately, now, when I run some simple test code, it crashes at runtime with an unhandled exception, but only when I call lua_close(). Here's the code:


#include <iostream>
using namespace std;
extern "C"{
#include <lua.hpp>
}
int main()
{

lua_State *L = luaL_newstate();

if(!L)
return -1;

luaL_dostring(
L,
"function add(first, second)\n"
" return first + second\n"
"end\n"
);

lua_close(L); /*Unhandled exception at 0x100042C0 (Lua.dll) in one more gain.exe: 0xC0000005: Access violation writing location 0x6D79F934.*/

cout << "Hello";

system("PAUSE");
return 0;
}


I've seen that sort of error before. I guess I'm accessing a null pointer? Yes the code gets past if(!L). Does anybody know what's going on?

EDIT: Screenshot added
Advertisement
Good old "Access Violation Error": Trying to access something that's out of the page or do something that a page isn't authorized for. Sometimes it can be trying to free a pointer that's not allocated. Possibly Lua is being de-initialized and then trying to free an internal pointer or something. If you've got everything set up right then it may actually be a Lua code problem. I had a similar problem with IrrKlang a while back and they fixed it in the next release (after I spent 5 hours trying to find the problem).

Check the Lua site and see if there's a newer version or else maybe a ticket in the bug-tracker.

Apart from that I think we'd need to know more about what's going on at your end. Does the Lua embedding doc say anything about necessary compiler or linker settings? Do you really need to extern "C" an .hPP file? What is the returned value from dostring()?

Poking around a little on google, do you need to call "luaL_openlibs(L)" after creating the state?
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
"it may actually be a Lua code problem"

It's much more likely to be a setup problem.

What does it do if you remove the luaL_dostring?

This topic is closed to new replies.

Advertisement