From 7eae6cd310e1c19cdb3029331f51efe908f53744 Mon Sep 17 00:00:00 2001 From: lawnjelly Date: Wed, 8 Dec 2021 09:47:28 +0000 Subject: [PATCH] RID tracked handles better error messages for dangling RIDs Keeps track of previous allocations as well as current allocations, as the previous allocation will be the one of interest in many cases for dangling RIDs. Prints the file / line number in the error message, instead of using print_verbose. --- core/rid_handle.cpp | 16 +++++++++++++--- core/rid_handle.h | 5 +++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/core/rid_handle.cpp b/core/rid_handle.cpp index d6bb482fd6db..1f7f1e905484 100644 --- a/core/rid_handle.cpp +++ b/core/rid_handle.cpp @@ -73,9 +73,15 @@ String RID_Database::_rid_to_string(const RID &p_rid, const PoolElement &p_pe) { s += "PE [ rev " + itos(p_pe.revision) + " ] "; #ifdef RID_HANDLE_ALLOCATION_TRACKING_ENABLED if (p_pe.filename) { - s += String(p_pe.filename) + " "; + s += String(p_pe.filename).get_file() + " "; } s += "line " + itos(p_pe.line_number); + + if (p_pe.previous_filename) { + s += " ( prev "; + s += String(p_pe.previous_filename).get_file() + " "; + s += "line " + itos(p_pe.previous_line_number) + " )"; + } #endif return s; } @@ -186,6 +192,11 @@ void RID_Database::handle_make_rid(RID &r_rid, RID_Data *p_data, RID_OwnerBase * r_rid._revision = pe->revision; #ifdef RID_HANDLE_ALLOCATION_TRACKING_ENABLED + // make a note of the previous allocation - this isn't super necessary + // but can pinpoint source allocations when dangling RIDs occur. + pe->previous_filename = pe->filename; + pe->previous_line_number = pe->line_number; + pe->line_number = 0; pe->filename = nullptr; #endif @@ -230,8 +241,7 @@ RID_Data *RID_Database::handle_get_or_null(const RID &p_rid) { const PoolElement &pe = _pool[p_rid._id]; if (pe.revision != p_rid._revision) { - print_verbose("RID revision incorrect : " + _rid_to_string(p_rid, pe)); - ERR_FAIL_COND_V_MSG(pe.revision != p_rid._revision, nullptr, "RID_Database get_or_null, revision is incorrect, object possibly freed before use."); + ERR_FAIL_COND_V_MSG(pe.revision != p_rid._revision, nullptr, "RID get_or_null, revision is incorrect, possible dangling RID. " + _rid_to_string(p_rid, pe)); } return pe.data; diff --git a/core/rid_handle.h b/core/rid_handle.h index a033b6fd0d95..fa9b55195f4a 100644 --- a/core/rid_handle.h +++ b/core/rid_handle.h @@ -119,9 +119,14 @@ class RID_Database { RID_Data *data; uint32_t revision; #ifdef RID_HANDLE_ALLOCATION_TRACKING_ENABLED + // current allocation uint16_t line_number; uint16_t owner_name_id; const char *filename; + + // previous allocation (allows identifying dangling RID source allocations) + const char *previous_filename; + uint32_t previous_line_number; #endif };