From 81d7fae644b759dd7b5620c7469b2941743dced8 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 3 Apr 2024 15:52:46 -0400 Subject: Introduce LLInstanceTracker::destroy() methods; use in ~LuaState(). --- indra/llcommon/llinstancetracker.h | 52 ++++++++++++++++++++++++++++++++++++++ indra/llcommon/lua_function.cpp | 11 ++------ 2 files changed, 54 insertions(+), 9 deletions(-) (limited to 'indra/llcommon') diff --git a/indra/llcommon/llinstancetracker.h b/indra/llcommon/llinstancetracker.h index 27422e1266..22e5d9c7a7 100644 --- a/indra/llcommon/llinstancetracker.h +++ b/indra/llcommon/llinstancetracker.h @@ -275,6 +275,35 @@ protected: public: virtual const KEY& getKey() const { return mInstanceKey; } + /// for use ONLY for an object we're sure resides on the heap! + static bool destroy(const KEY& key) + { + return destroy(getInstance(key)); + } + + /// for use ONLY for an object we're sure resides on the heap! + static bool destroy(const weak_t& ptr) + { + return destroy(ptr.lock()); + } + + /// for use ONLY for an object we're sure resides on the heap! + static bool destroy(const ptr_t& ptr) + { + if (! ptr) + { + return false; + } + + // Because we store and return ptr_t instances with no-op deleters, + // merely resetting the last pointer doesn't destroy the referenced + // object. Don't even bother resetting 'ptr'. Just extract its raw + // pointer and delete that. + auto raw{ ptr.get() }; + delete raw; + return true; + } + private: LLInstanceTracker( const LLInstanceTracker& ) = delete; LLInstanceTracker& operator=( const LLInstanceTracker& ) = delete; @@ -479,6 +508,29 @@ public: template using key_snapshot_of = instance_snapshot_of; + /// for use ONLY for an object we're sure resides on the heap! + static bool destroy(const weak_t& ptr) + { + return destroy(ptr.lock()); + } + + /// for use ONLY for an object we're sure resides on the heap! + static bool destroy(const ptr_t& ptr) + { + if (! ptr) + { + return false; + } + + // Because we store and return ptr_t instances with no-op deleters, + // merely resetting the last pointer doesn't destroy the referenced + // object. Don't even bother resetting 'ptr'. Just extract its raw + // pointer and delete that. + auto raw{ ptr.get() }; + delete raw; + return true; + } + protected: LLInstanceTracker() { diff --git a/indra/llcommon/lua_function.cpp b/indra/llcommon/lua_function.cpp index 7a5668f384..c86faf1ae2 100644 --- a/indra/llcommon/lua_function.cpp +++ b/indra/llcommon/lua_function.cpp @@ -286,7 +286,7 @@ LLSD lua_tollsd(lua_State* L, int index) popper.disarm(); // Table keys are all integers: are they reasonable integers? // Arbitrary max: may bite us, but more likely to protect us - size_t array_max{ 10000 }; + const size_t array_max{ 10000 }; if (keys.size() > array_max) { return lluau::error(L, "Conversion from Lua to LLSD array limited to %d entries", @@ -459,14 +459,7 @@ LuaState::~LuaState() { // Did somebody call obtainListener() on this LuaState? // That is, is there a LuaListener key in its registry? - auto listener{ getListener() }; - if (listener) - { - // if we got a LuaListener instance, destroy it - auto lptr{ listener.get() }; - listener.reset(); - delete lptr; - } + LuaListener::destroy(getListener()); lua_close(mState); -- cgit v1.2.3