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

Controlling lifetime of Lua references #1467

Open
nimble0 opened this issue Mar 26, 2023 · 1 comment
Open

Controlling lifetime of Lua references #1467

nimble0 opened this issue Mar 26, 2023 · 1 comment

Comments

@nimble0
Copy link

nimble0 commented Mar 26, 2023

I'm looking for a way to invalidate Lua references so I can match their lifetimes to the CPP object they point to.

It looks like manually setting the metatable to nil is one option:

{
	Test test;
	test.a = 54;

	lua["test"] = &test;
	sol::userdata testRef = lua["test"];

	lua.script(R"(
		print(test, test.a)
	)");

	testRef[sol::metatable_key] = sol::nil;
}

// script error instead of dereferencing a dangling pointer
lua.script(R"(
	print(test, test.a)
)");

Are there any problems with doing this? Is there a better way?

@Rochet2
Copy link

Rochet2 commented Mar 26, 2023

You may be interested in this issue as well #1367

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