vinniefalco/LuaBridge

Lua_Ref Func Access violation

yatoiv opened this issue · 1 comments

Ok so I'm a student and trying to make a basic game that can be custom coded using lua and I'm using luabridge
and i have a custom class that allows the user to render his custom objects

std::vector<luabridge::LuaRef> RenderHooks = std::vector<luabridge::LuaRef>();

class RenderManager {
public:
	void OnRender(luabridge::LuaRef Func) {
		printf("Added a hook\n");
		RenderHooks.push_back(Func);
		printf("Hooks Count: %i\n", RenderHooks.size());
	}
	bool ClearHooks() {
		RenderHooks.clear();
		return true;
	}
	
	void CallHooks() {
		printf("Hooks Count: %i\n", RenderHooks.size());
		for (luabridge::LuaRef hook : RenderHooks) {
			try {
				// call hook
				printf("Calling a hook\n");
				hook();
			}
			catch (luabridge::LuaException const& e) {
				std::cout << e.what() << std::endl;
			}

		}
	}
};

and i use

		.beginClass<RenderManager>("RenderManagerClass")
		.addFunction("OnRender", &RenderManager::OnRender)
		.addFunction("ClearHooks", &RenderManager::ClearHooks)
                .endClass()
		.addVariable("RenderManager", &renderMng, false)
		.endNamespace();

to register it

so i can use

render = function()
    print("a message from lua")
end

Game.RenderManager:OnRender(render)

while true do 
   -- Some game calculations happen here and it can run forever
end

but the issue is when i try to run renderMng.CallHooks() which is supposed to trigger the lua function defined earlier i get the following issue

LuaException: missing error

and access violation some times and the lua function never gets triggered and after a few more calls the app exits when debugging i get

Exception thrown at 0x00007FFDC54EFFA6 (msvcrt.dll) in gamebase.exe: 0xC0000005: Access violation reading location 0x0000000065BCF000.
another run
Exception thrown at 0x00007FFDC552D2D0 (msvcrt.dll) in gamebase.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
My console before it exits
image

0.157??
image

which seems to be triggered here
image

I tried calling the function using luabridge::getGlobal(L, "funcname"); and same issue

it seems to be working as expected if i remove my while true loop that I have, but there are some important calculations that are being ran on it so I cannot remove it

Its my first time using luabridge and i don't know what am i wrong at

I'm using luabridge2 from vcpkg
and Lua 5.4.4

Not sure what are you doing wrong, but it works fine for me (filling the voids you left from the repro case you posted here)