Skip to content

Commit

Permalink
Add additional checks to shared_ptr test.
Browse files Browse the repository at this point in the history
This tests the issue reported here: Oberon00#4

Credits to @Oberon00
  • Loading branch information
nitrocaster committed May 2, 2020
1 parent 4cc09ed commit b1135f9
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions test/test_shared_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ struct X
{
X(int value)
: value(value)
{}
{
alive++;
}

~X()
{
alive--;
}

int value;
static int alive;
};

int X::alive = 0;

int get_value(std::shared_ptr<X> const& p)
{
return p->value;
Expand All @@ -25,6 +35,11 @@ std::shared_ptr<X> filter(std::shared_ptr<X> const& p)
return p;
}

std::shared_ptr<X> make_x()
{
return std::shared_ptr<X>(luabind::luabind_new<X>(0), luabind::luabind_delete<X>);
}

void test_main(lua_State* L)
{
using namespace luabind;
Expand All @@ -33,7 +48,8 @@ void test_main(lua_State* L)
class_<X>("X")
.def(constructor<int>()),
def("get_value", &get_value),
def("filter", &filter)
def("filter", &filter),
def("make_x", &make_x)
];

DOSTRING(L,
Expand All @@ -43,6 +59,25 @@ void test_main(lua_State* L)

DOSTRING(L,
"assert(x == filter(x))\n"
"x = nil\n"
"collectgarbage()\n"
);
DOSTRING(L,
"sx = make_x()\n"
"function get_sx() return sx end"
);
std::shared_ptr<X> spx = call_function<std::shared_ptr<X>>(L, "get_sx");
TEST_CHECK(spx.use_count() == 2);
DOSTRING(L,
"sx = nil\n"
"collectgarbage()\n"
);
TEST_CHECK(spx.use_count() == 1);
TEST_CHECK(X::alive == 1);
spx.reset(); // reference to lua object is released here
DOSTRING(L,
"collectgarbage()\n" // unreferenced object is collected
);
TEST_CHECK(X::alive == 0);
}

0 comments on commit b1135f9

Please sign in to comment.