Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua_Ref Func Access violation #301

Open
yatoiv opened this issue Nov 5, 2022 · 1 comment
Open

Lua_Ref Func Access violation #301

yatoiv opened this issue Nov 5, 2022 · 1 comment

Comments

@yatoiv
Copy link

yatoiv commented Nov 5, 2022

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

@kunitoki
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants