mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Added test for returning a const ref of usertype.
And consequently having sol free up the memory of the reference when doing garbage collection.
This commit is contained in:
parent
12d6573a10
commit
898a4ba990
@ -289,3 +289,31 @@ TEST_CASE("usertype/const-pointer", "Make sure const pointers can be taken") {
|
||||
REQUIRE(x == 201);
|
||||
std::cout << "----- end of 6" << std::endl;
|
||||
}
|
||||
|
||||
TEST_CASE("usertype/const-reference-no-collect", "Make sure const reference aren't collected") {
|
||||
|
||||
struct Foo {
|
||||
bool allow_destruction = false;
|
||||
~Foo() {
|
||||
REQUIRE(allow_destruction);
|
||||
}
|
||||
};
|
||||
|
||||
Foo foo;
|
||||
|
||||
sol::state lua;
|
||||
lua.new_usertype<Foo>("Foo");
|
||||
lua["getFooPtr"] = [&](){ return &foo;};
|
||||
lua["getFooRef"] = [&]() -> Foo& { return foo;};
|
||||
lua["getFooRefConst"] = [&]() -> const Foo& { return foo;};
|
||||
lua.safe_script("getFooPtr()");
|
||||
//Should not collect anything.
|
||||
lua.collect_gc();
|
||||
lua.safe_script("getFooRef()");
|
||||
//Should not collect anything.
|
||||
lua.collect_gc();
|
||||
lua.safe_script("getFooRefConst()");
|
||||
//Should not collect anything. But it does!
|
||||
lua.collect_gc();
|
||||
foo.allow_destruction = true;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user