• Advertisement
Sign in to follow this  

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

This topic is 1903 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

After a bit of a struggle, I've gotten Lua to work with [b]Visual Studio 2012 (Lua 5.2)[/b]. 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();

return -1;

"function add(first, second)\n"
" return first + second\n"

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

cout << "Hello";

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

Share this post

Link to post
Share on other sites
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 .h[u]PP[/u] 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? Edited by Khatharr

Share this post

Link to post
Share on other sites
"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?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement