From 89107d3b90110af1182ca24c28a397b65ea8bf85 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Mon, 19 Sep 2016 00:36:50 -0400 Subject: [PATCH] Fix crashing of simple usertypes from a previous herpderp. --- single/sol/sol.hpp | 8 ++++---- sol/function_types.hpp | 4 ++-- test_simple_usertypes.cpp | 31 +++++++++++++++++++++++++++++++ test_usertypes.cpp | 31 +++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 6 deletions(-) diff --git a/single/sol/sol.hpp b/single/sol/sol.hpp index 0b1a1b11..4dc7c3bf 100644 --- a/single/sol/sol.hpp +++ b/single/sol/sol.hpp @@ -20,8 +20,8 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // This file was generated with a script. -// Generated 2016-09-18 02:39:04.820718 UTC -// This header was generated with sol v2.14.0 (revision 1dfeb1d) +// Generated 2016-09-19 04:36:16.270592 UTC +// This header was generated with sol v2.14.2 (revision 431c629) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_HPP @@ -8448,8 +8448,8 @@ namespace sol { template struct pusher>> { - static int push(lua_State* L, detail::tagged>) { - lua_CFunction cf = detail::user_alloc_destroy; + static int push(lua_State* L, destructor_wrapper) { + lua_CFunction cf = detail::usertype_alloc_destroy; return stack::push(L, cf); } }; diff --git a/sol/function_types.hpp b/sol/function_types.hpp index a2eba3ec..4dde4682 100644 --- a/sol/function_types.hpp +++ b/sol/function_types.hpp @@ -321,8 +321,8 @@ namespace sol { template struct pusher>> { - static int push(lua_State* L, detail::tagged>) { - lua_CFunction cf = detail::user_alloc_destroy; + static int push(lua_State* L, destructor_wrapper) { + lua_CFunction cf = detail::usertype_alloc_destroy; return stack::push(L, cf); } }; diff --git a/test_simple_usertypes.cpp b/test_simple_usertypes.cpp index 36b8415f..d8e150d3 100644 --- a/test_simple_usertypes.cpp +++ b/test_simple_usertypes.cpp @@ -406,3 +406,34 @@ TEST_CASE("usertype/simple-runtime-append", "allow extra functions to be appende REQUIRE(z == 100); REQUIRE(w == 100); } + +TEST_CASE("usertype/simple-destruction-test", "make sure usertypes are properly destructed and don't double-delete memory or segfault") { + sol::state lua; + + class CrashClass { + public: + CrashClass() { + } + + ~CrashClass() { + a = 10; // This will cause a crash. + } + + private: + int a; + }; + + lua.new_simple_usertype("CrashClass", + sol::call_constructor, sol::constructors>() + ); + + lua.script(R"( + function testCrash() + local x = CrashClass() + end + )"); + + for (int i = 0; i < 1000; ++i) { + lua["testCrash"](); + } +} diff --git a/test_usertypes.cpp b/test_usertypes.cpp index 9dff3238..e800e7f1 100644 --- a/test_usertypes.cpp +++ b/test_usertypes.cpp @@ -1393,3 +1393,34 @@ TEST_CASE("usertype/as_function", "Ensure that variables can be turned into func REQUIRE(x == 24); REQUIRE(y == 24); } + +TEST_CASE("usertype/destruction-test", "make sure usertypes are properly destructed and don't double-delete memory or segfault") { + sol::state lua; + + class CrashClass { + public: + CrashClass() { + } + + ~CrashClass() { + a = 10; // This will cause a crash. + } + + private: + int a; + }; + + lua.new_usertype("CrashClass", + sol::call_constructor, sol::constructors>() + ); + + lua.script(R"( + function testCrash() + local x = CrashClass() + end + )"); + + for (int i = 0; i < 1000; ++i) { + lua["testCrash"](); + } +}