Skip to main content
gamedev.net
🔒 Locked

LUA Problem - Driving Me Mad

Started by brcolow 49 replies 8.2k views
Original Post
brcolow
brcolow
Alright, this LUA problem has been driving me absolutley mad for the past few days, so I have come to the community for help :). My setup: I am running LUA 5.0 with tolua++ and binding my CMapManager class to LUA. I have created a nice and easy interface for LUA here:

#include "CLuaManager.h"

CLuaManager::CLuaManager(CGameManager* gameManager) : gameManager(gameManager)
{

}

//Open new state, used first for ANYTHING LUA related
void CLuaManager::LuaCreateState()
{
	//Create LUA state
	LuaState = lua_open();

	if (NULL == LuaState)
		{
	      gameManager->log << "Error Initializing lua\n";
		}

}


//Destory current open state
void CLuaManager::LuaDestroyState()
{
	//Close LUA state
	lua_close(LuaGetState());
}

//Return current state
lua_State* CLuaManager::LuaGetState()
{

	return LuaState;

}

//Load .lua external script
void CLuaManager::LuaLoadScript(const char * luaScript)
{
	if (lua_dofile(LuaState, luaScript))
	{
		gameManager->log <<"YAY\n";
	}
	else
	{
		gameManager->log <<"oh noes!\n";
	}

	
}

void CLuaManager::LuaOpenModules()
{
	//Open all modules for LUA Binding
	//tolua_CWeaponManager_open(LuaGetState());
	//tolua_CDialogueManager_open(LuaGetState());
	//tolua_CItemManager_open(LuaGetState());
	//tolua_CLogger_open(LuaGetState());
	tolua_CMapManager_open(LuaGetState());
	//tolua_COpenALManager_open(LuaGetState());
	//tolua_XML_open(LuaGetState());
}


//Open LUA Libraries for extended functionality
void CLuaManager::LuaOpenLibs()
{

	lua_baselibopen(LuaGetState());
	lua_iolibopen(LuaGetState());
	lua_strlibopen(LuaGetState());
	lua_mathlibopen(LuaGetState());

}
Pretty damn straight foward, heh. Now in my main file I do the following:

	gameManager->luaMgr->LuaCreateState();
	gameManager->luaMgr->LuaOpenLibs();
	gameManager->luaMgr->LuaOpenModules();
	gameManager->luaMgr->LuaLoadScript("test.lua");

Now, here is the script file "test.lua":

mapMgr = CMapManager:new()
mapMgr:loadMap("demo_test/Terrain Layers/Layer6.bmp", "demo_test")
The Problem: My program crashes at the lua_dofile line. Using the stack it starts at _file.c, then fprintf.c, then goes to lua_dofile. It's crazy, and I am so confused. Help! :( Thank you! Mike
-Brcolow-
brcolow
brcolow
I don't believe LUA ends with ";" but I did add it, and still the same error. What additional info do you require?
-Brcolow-
hpolloni
hpolloni
btw lua_dofile return 0 in case of success and an error code (non-zero) in case of an error, so the if statement is

if(!lua_dofile(blabalala
tentoid
tentoid
if you have just print("Hello") in your test.lua will your app crash also then?
brcolow
brcolow
@hpollini: Alright, I changed the if statement.

@tentoid: Yes, even with just that it still crashes:


Quote:

ntdll.dll!7c901095()
> msvcr71d.dll!_lock_file(void * pf=0x0043e23c) Line 236 C
msvcr71d.dll!fputs(const char * string=0x063f8648, _iobuf * stream=0x0043e23c) Line 53 + 0x9 C
OninWars.exe!_luaopen_io() + 0x19e
OninWars.exe!_luaD_precall() + 0x160
OninWars.exe!_luaV_execute() + 0x9fd
OninWars.exe!_luaD_call() + 0x4b
OninWars.exe!_lua_pcall() + 0x75
OninWars.exe!_luaD_rawrunprotected() + 0x3a
OninWars.exe!_luaD_pcall() + 0x41
OninWars.exe!_lua_pcall() + 0x55
OninWars.exe!_lua_dofile() + 0x3a
OninWars.exe!_lua_dofile() + 0x17
OninWars.exe!CLuaManager::LuaLoadScript(const char * luaScript=0x0043e320) Line 44 + 0xf C++
OninWars.exe!main() Line 29 C++
OninWars.exe!mainCRTStartup() Line 398 + 0x11 C
kernel32.dll!7c816d4f()
kernel32.dll!7c8399f3()



Same as before.
-Brcolow-
algorhythmic
algorhythmic
put the source up online
[email=algorhythmic@transcendenz.co.uk]algorhythmic[/email] | home | xltronic | whatever you do will be insignificant, but it is very important that you do it - mahatma gandhi
brcolow
brcolow
What source? I showed you the source.
-Brcolow-
brcolow
brcolow
Any other ideas on this? Any more information needed?

Thanks,
Mike
-Brcolow-
Sneftel
Sneftel
Quote:

OninWars.exe!_lua_pcall() + 0x55
OninWars.exe!_lua_dofile() + 0x3a
OninWars.exe!_lua_dofile() + 0x17
OninWars.exe!CLuaManager::LuaLoadScript(const char * luaScript=0x0043e320) Line 44 + 0xf C++

Something is very strange there.... lua_dofile calls aux_do, not lua_pcall (and certainly not itself). This is because you've applied optimizations that obscure the stack trace (aggressive inlining, whole-program optimization, etc). That's probably not the root of your problem, but it is hampering debugging. Recompile your program in Debug mode, and post the resultant stack trace.
brcolow
brcolow
Alrighty, here is the stack trace in debug mode:

ntdll.dll!7c901095()
> msvcr71d.dll!_lock_file(void * pf=0x004b93a8) Line 236 C
msvcr71d.dll!fprintf(_iobuf * str=0x004b93a8, const char * format=0x004d3074, ...) Line 63 + 0x9 C
OninWars.exe!_lua_dofile() + 0xaf
OninWars.exe!_lua_dofile() + 0x46
OninWars.exe!_lua_dofile() + 0x17
OninWars.exe!CLuaManager::LuaLoadScript(const char * luaScript=0x004b9588) Line 44 + 0xf C++
OninWars.exe!main() Line 29 C++
OninWars.exe!mainCRTStartup() Line 398 + 0x11 C
kernel32.dll!7c816d4f()
kernel32.dll!7c8399f3()
-Brcolow-
Sneftel
Sneftel
Um, that still seems extremely messed up. Are you sure you haven't done strange things to your debug configuration, setting optimization options in it?
brcolow
brcolow
I'm pretty sure heh...so are you telling me that there are no problems with the actual source though?
-Brcolow-
Sneftel
Sneftel
It's impossible to tell without seeing a good stack trace. What I'm guessing is that the VM is trying to report an error, and crashing while doing so, but I can't verify that without seeing whether it passed through an error-reporting call.
hplus0603
hplus0603
That makes a lot of sense. do_file() enters an error, and calls into fprintf(), but the handle (probably stderr) hasn't been set up properly.

If you're a windows app (not console app) then I've seen cases before where writing to stdout or stderr will crash. Try doing freopen(stderr, "stderr.txt", "wb") and the same for stdout at the top of main() (before you do anything Lua related).

You could also just set a brekpoint in do_file() and single-step through it to figure out where it's going wrong. You know, like, what breakpoints were invented for ...
enum Bool { True, False, FileNotFound };
Sneftel
Sneftel
Exactly. Ironically, embedded Lua tends to crap out while reporting errors.

I really wouldn't suggest single-stepping here, though. That would take _forever_. What might be more efficient, though, would be to switch into the fprintf frame during the crash and see what "format" is set to. It won't tell you exactly what's going wrong, but it'll at least give you the general category of the error. And if you post it here, I'll help you decode it. Of course, the best solution would be to un-mung your debug configuration so it didn't inline functions.
brcolow
brcolow
Alright:

format 0x004d3074 "%s(theres actually a weird box character right here but it is edited out in this text area box)" const char *


Thats the type..I have NO idea what that means though, hehe.
-Brcolow-
Sneftel
Sneftel
that would be the callalert() function in lauxlib.c. You can put a breakpoint in it and step through it, monitoring what error message is returned from lua_tostring.
Feral
Feral
From: Lua 5.0 Reference Manual
3.15 - Protected Calls...The lua_pcall function returns 0 in case of success or one of the following error codes (defined in lua.h):    * LUA_ERRRUN --- a runtime error.    * LUA_ERRMEM --- memory allocation error. For such errors, Lua does not call the error handler function.    * LUA_ERRERR --- error while running the error handler function. 



The 'wierd box character' is just '\n', specifically the font you are looking at is using a 'wierd box character' as a place holder graphic (=

You might try:
      if  (   lua_load(L,getF,pFile, strChunkName.c_str() )          ||  lua_pcall(L,0,0,0)          )      {          // error handling      }

See Here for the rest if needed.

HTH
*edit* Oh, when using lua_pcall your error message is on the (at the top of the) lua stack: Use 'lua_tostring(L,-1)' to fetch it, i.e.
printf("Unexpected error, with:\r\n\"%s\"\r\nAborting.", lua_tostring(L,-1) );lua_close(L);L = NULL;// exit or whatever
brcolow
brcolow
Having never used a break point before (heh, I know), how could I put one in an external include such as lauxlib.c?
-Brcolow-

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.