Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Shaquil   Members   -  Reputation: 815

Like
0Likes
Like

Posted 02 November 2012 - 10:10 AM

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

Attached Thumbnails

  • screenshot.png


Sponsor:

#2 Khatharr   Crossbones+   -  Reputation: 2817

Like
0Likes
Like

Posted 03 November 2012 - 04:37 AM

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?

Edited by Khatharr, 03 November 2012 - 05:22 AM.

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.

#3 Katie   Members   -  Reputation: 1281

Like
0Likes
Like

Posted 03 November 2012 - 11:01 AM

"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?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS