From 0ccd559d176b5e0d0c72128c382b6cad525438c2 Mon Sep 17 00:00:00 2001 From: lawnjelly Date: Wed, 11 Sep 2024 15:18:13 +0100 Subject: [PATCH] `Object::call()` prevent debug lock accessing dangling pointer Self deleting an object within a call was leading to crashes due to referencing freed memory, due to a raw pointer stored in the debug lock. Co-authored-by: RandomShaper --- core/object.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/core/object.cpp b/core/object.cpp index 46287b000db..b41383938c5 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -43,14 +43,17 @@ #ifdef DEBUG_ENABLED struct _ObjectDebugLock { - Object *obj; + ObjectID obj_id; _ObjectDebugLock(Object *p_obj) { - obj = p_obj; - obj->_lock_index.ref(); + obj_id = p_obj->get_instance_id(); + p_obj->_lock_index.ref(); } ~_ObjectDebugLock() { - obj->_lock_index.unref(); + Object *obj_ptr = ObjectDB::get_instance(obj_id); + if (likely(obj_ptr)) { + obj_ptr->_lock_index.unref(); + } } };