From 21142e7e7d3f32baccfe385684a9338e8789fc7a Mon Sep 17 00:00:00 2001 From: ThePhD Date: Mon, 9 Dec 2013 12:05:17 -0500 Subject: [PATCH] Removed std::unordered_map storage on tables since they were getting deleted anyways. Memory leaks are currently present: will have to figure out how to patch those up. --- sol/table.hpp | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/sol/table.hpp b/sol/table.hpp index b849cc60..e1272208 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -42,11 +42,9 @@ namespace detail { } } class table : virtual public reference { -private: - std::unordered_map> funcs; public: - table() noexcept: reference(), funcs() {} - table(lua_State* L, int index = -1): reference(L, index), funcs() { + table() noexcept: reference() {} + table(lua_State* L, int index = -1): reference(L, index) { type_assert(L, index, type::table); } @@ -98,7 +96,7 @@ private: table& set_isfunction_fx(std::false_type, T&& key, TFx&& fx) { typedef typename std::decay::type clean_lambda; typedef typename detail::function_traits::free_function_pointer_type raw_func_t; - typedef std::is_convertible isconvertible; + typedef std::is_convertible isconvertible; return set_isconvertible_fx(isconvertible(), std::forward(key), std::forward(fx)); } @@ -190,31 +188,33 @@ private: template table& set_fx(T&& key, std::unique_ptr luafunc) { std::string fkey(key); - auto hint = funcs.find(fkey); - if (hint == funcs.end()) { - std::shared_ptr sptr(luafunc.release()); - hint = funcs.emplace_hint(hint, fkey, std::move(sptr)); - } - else { - hint->second.reset(luafunc.release()); - } - lua_func* target = hint->second.get(); + + lua_func* target = luafunc.release(); void* userdata = reinterpret_cast(target); lua_CFunction freefunc = &lua_func::call; - const char* freefuncname = hint->first.c_str(); - const luaL_Reg funcreg[ 2 ] = { + const char* freefuncname = fkey.c_str(); + const luaL_Reg funcreg[2] = { { freefuncname, freefunc }, { nullptr, nullptr } }; + + push(); - push(); - - stack::push(state(), userdata); - luaL_setfuncs(state(), funcreg, 1); - + // Actual function call we want + stack::push( state(), userdata ); + luaL_setfuncs(state(), funcreg, 1); + lua_pop(state(), 1); return *this; } + + static int destroyfunc( lua_State* L ) { + void* userdata = lua_touserdata( L, 1 ); + lua_func* ptr = static_cast( userdata ); + std::default_delete dx{ }; + dx( ptr ); + return 0; + } }; } // sol