Weird Lua Access Violations.

BlitzMax Forums/BlitzMax Programming/Weird Lua Access Violations.

Galaxy613(Posted 2011) [#1]
Has anyone else encountered a problem anything like this? I have tried searching for anyone else who has had a EXCEPTION_ACCESS_VIOLATION while using lua_pcall()/lua_call()?

It's both oddly specific and not specific at the same time. I'm using pub.lua with lua version 5.1.4 and I am just getting bullets in... Now I have set up a hook system that you give the name of the global function and the name of the hook and it'll add them to the hook list, so when inside my code I want to call all associated Global Lua Functions connected to that hook name, it'll go through the hook list and see.

It's been working fine... until I started getting my bullets working. I've talked a lot about my hook system but I don't think it's the problem.

*Edit* It seems to be primarily the fault of my code that calls the double argument hooks. This is the code I'm using to call the hook and pass the two bmax objects:

		For Local hook:_Hook = EachIn HookList
		
			If hook.Name = hookname
			'	If KeyHit(KEY_P) Then Notify hookname + " [ " + hook.func
				lua_getglobal( luaState, hook.func )
				
				If lua_type( luaState, -1 ) = LUA_TFUNCTION Then
					lua_pushbmaxobject(luaState,obj)
					lua_pushbmaxobject(luaState,obj2)
					If lua_pcall( luaState, 2, 1, 0 ) Then
						scriptError = lua_tostring( luaState, -1 )
						lua_pop( luaState, 1 )
						scriptIsGood = False
					Else
						lua_pop( luaState, 1 )
					EndIf
			'	ElseIf lua_type (luaState, -1) = LUA_TNIL
			'		If KeyHit(KEY_O) Then Notify hookname + " [ " + hook.func + " ] Is Nil"
				EndIf
				
				lua_pop( luaState, 1 )
			End If
			
		Next


When it crashes, it's either on "lua_pushbmaxobject(luaState,obj)" or "If lua_pcall( luaState, 2, 1, 0 ) Then"...

And somtimes, it doesn't crash at all connected with the double argument bullet hook, sometimes it crashes on my generic drawing hook which passes no objects. It crashes at "If lua_pcall( luaState, 0, 1, 0 ) Then"

Is there someway to get actual information from "EXCEPTION_ACCESS_VIOLATION" through some Lua libraries or something? It doesn't tell me anything and I highly suspect something else then the actual line of code is the culprit.. :|


Zeke(Posted 2011) [#2]
that is because after calling function you pop 2 elements from stack (lua_call/pcall pops function and arguments)

For Local hook:_Hook = EachIn HookList
		
	If hook.Name = hookname
		lua_getglobal( luaState, hook.func )
				
		If lua_type( luaState, -1 ) = LUA_TFUNCTION Then
			lua_pushbmaxobject(luaState,obj)
			lua_pushbmaxobject(luaState,obj2)
			If lua_pcall( luaState, 2, 1, 0 ) Then
				scriptError = lua_tostring( luaState, -1 )
				lua_pop( luaState, 1 ) 'pop error string
				scriptIsGood = False
			Else
				lua_pop( luaState, 1 ) 'pop returned value
			EndIf
			'lua_pcall pops function and arguments so no need pop
		Else
			lua_pop( luaState, 1 ) 'was not function
		EndIf
	End If	
Next



Galaxy613(Posted 2011) [#3]
Yep! I suppose I was popping too many off the stack so much it crashed. Everything seems stable now and I also updated my other hooks callers in accordance. Thanks a LOT Zeke! :)