From a34020ba21777c21f57e3bb765d083b38ae9ad1e Mon Sep 17 00:00:00 2001 From: Rene Gollent Date: Thu, 6 Dec 2012 17:57:14 -0500 Subject: [PATCH] A bit more fine-tuning to BReferenceable debugging. - Rework quick stack range check as suggested by Ingo. - If the ref count is > 1 we invoke the debugger unconditionally. - If equal to 1, we first perform a quick heuristic check to see if the var might be on the stack. If we can't conclusively determine that is, we make certain by comparing to the thread's actual stack range. --- src/kits/support/Referenceable.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/kits/support/Referenceable.cpp b/src/kits/support/Referenceable.cpp index 39cc641f33..1aaedec5d9 100644 --- a/src/kits/support/Referenceable.cpp +++ b/src/kits/support/Referenceable.cpp @@ -27,7 +27,8 @@ BReferenceable::BReferenceable() BReferenceable::~BReferenceable() { #ifdef DEBUG - if (fReferenceCount != 0) { + bool enterDebugger = false; + if (fReferenceCount == 1) { // Simple heuristic to test if this object was allocated // on the stack: check if this is within 1KB in either // direction of the current stack address, and the reference @@ -35,10 +36,22 @@ BReferenceable::~BReferenceable() // imply the object was allocated/destroyed on the stack // without any references being acquired or released. char test; - int64 testOffset = (int64)this - (int64)&test; - if (testOffset < -1024 || testOffset > 1024 || fReferenceCount != 1) - debugger("Deleted referenceable object with non-zero ref count."); - } + size_t testOffset = (addr_t)this - (addr_t)&test; + if (testOffset > 1024 || -testOffset > 1024) { + // might still be a stack object, check the thread's + // stack range to be sure. + thread_info info; + status_t result = get_thread_info(find_thread(NULL), &info); + if (result != B_OK || this < info.stack_base + || this > info.stack_end) { + enterDebugger = true; + } + } + } else if (fReferenceCount != 0) + enterDebugger = true; + + if (enterDebugger) + debugger("Deleted referenceable object with non-zero ref count."); #endif }