Jump to content
  • Advertisement
Sign in to follow this  
theMadHatter

printing from lua crashes

This topic is 3249 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

Hello, I have the following code:
int main() {
	// Create a new lua state
	lua_State *myLuaState = lua_open();

	// Connect LuaBind to this lua state
	luabind::open(myLuaState);

	luaL_dofile(myLuaState, "main.lua");

	luabind::call_function<void>(myLuaState, "main");

	lua_close(myLuaState);
}
And here is my lua code:
function main()
	print("hello")
end
When this code runs however, I get a debug error saying "the program has requested the runtime to terminate in an unusual way". It does the same if I use io.write instead. Is there something special I have to do? I can make my own print function but luabind doesn't seem to like vararg functions(?). edit: I just found out that no matter what function I call from inside lua, it crashes. I tried this.... x = math.random(1, 10) And it crashed. Any ideas? [Edited by - theMadHatter on October 27, 2009 6:51:32 PM]

Share this post


Link to post
Share on other sites
Advertisement
How are u linking in Lua ? Building it within your project? Linking in as a dll? or lib? If you link in with incompatible runtimes it will crash when u try to execute any Lua code.

-ddn

Share this post


Link to post
Share on other sites
I'm linking with libs. The ones that came with luabind.
I can execute lua code.

int main() {
// Create a new lua state
lua_State *myLuaState = lua_open();

// Connect LuaBind to this lua state
luabind::open(myLuaState);

luaL_dofile(myLuaState, "main.lua");

luabind::call_function<void>(myLuaState, "main");

cout << luabind::globals(myLuaState)["x"] << endl;

lua_close(myLuaState);
}

with

function main()
x = 3
end


works. But whenever I try to call a built in lua function it crashes. I can make my own functions.
Like

function main()
x = 3
change()
end

function change()
x = 4
end

works.

edit:
I've tested a few things with luaL_dostring and I found that
luaL_dostring(myLuaState, "x = 3");
cout << laubind::globals(myLuaState)["x"] << endl;
works as intended , prints out a 3.
BUT
luaL_dostring(myLuaState, "x = math.random(1, 10)");
cout << laubind::globals(myLuaState)["x"] << endl;
prints out a blank. It doesn't crash however.
Also when I wrap a try statement around the call_function it produces a 'lua runtime error' exception, but when I wrap the dostring it doesn't produce any exception. Not sure if this is to be expected or not (ie - maybe tostring isn't supposed to send exceptions).
edit2:
using visual studio 2008 if that matters at all

[Edited by - theMadHatter on October 27, 2009 7:09:19 PM]

Share this post


Link to post
Share on other sites
I don't know if luabind does it automatically, but I see you're not calling luaL_openlibs to open all of the standard library functions.

Also, you should be explicitly catching the luabind::error exception (The "the program has requested the runtime to terminate in an unusual way" error means you had an unhandled exception). You can use the what() method to find out the details of the error:
try
{
// lua stuff
}
catch(luabind::error &e)
{
std::cout << e.what() << std::endl;
}

Share this post


Link to post
Share on other sites
OOOH my gosh thank you.
I knew there was a function like that in my brief encounter with angel code. But I never saw that function called in all the examples with luabind, or atleast my eyes skipped over it.
Thanks a bunch!
Now I go crawl into my hole...

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!