From b1135f9e3f1da2e5af5a2a93664c788f44671316 Mon Sep 17 00:00:00 2001 From: Pavel Kovalenko Date: Sun, 3 May 2020 01:14:27 +0300 Subject: [PATCH] Add additional checks to shared_ptr test. This tests the issue reported here: https://github.com/Oberon00/luabind/issues/4 Credits to @Oberon00 --- test/test_shared_ptr.cpp | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/test/test_shared_ptr.cpp b/test/test_shared_ptr.cpp index 1a7cccff..f2509ea0 100644 --- a/test/test_shared_ptr.cpp +++ b/test/test_shared_ptr.cpp @@ -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 const& p) { return p->value; @@ -25,6 +35,11 @@ std::shared_ptr filter(std::shared_ptr const& p) return p; } +std::shared_ptr make_x() +{ + return std::shared_ptr(luabind::luabind_new(0), luabind::luabind_delete); +} + void test_main(lua_State* L) { using namespace luabind; @@ -33,7 +48,8 @@ void test_main(lua_State* L) class_("X") .def(constructor()), def("get_value", &get_value), - def("filter", &filter) + def("filter", &filter), + def("make_x", &make_x) ]; DOSTRING(L, @@ -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 spx = call_function>(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); }