Permalink
Browse files

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.
  • Loading branch information...
1 parent cb44a2a commit a34020ba21777c21f57e3bb765d083b38ae9ad1e @anevilyak anevilyak committed Dec 6, 2012
Showing with 18 additions and 5 deletions.
  1. +18 −5 src/kits/support/Referenceable.cpp
@@ -27,18 +27,31 @@ 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
// count is 1. If so, we don't flag a warning since that would
// 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
}

0 comments on commit a34020b

Please sign in to comment.