Make BBoxHierarchy ref-counted, fix leak in RTreeTest.
Review URL: https://codereview.appspot.com/6489108

git-svn-id: http://skia.googlecode.com/svn/trunk@5484 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/bench/RTreeBench.cpp b/bench/RTreeBench.cpp
index edea57d..d13d887 100644
--- a/bench/RTreeBench.cpp
+++ b/bench/RTreeBench.cpp
@@ -37,7 +37,7 @@
         }
     }
     virtual ~BBoxBuildBench() {
-        delete fTree;
+        fTree->unref();
     }
 protected:
     virtual const char* onGetName() {
@@ -93,7 +93,7 @@
         fTree->flushDeferredInserts();
     }
     virtual ~BBoxQueryBench() {
-        delete fTree;
+        fTree->unref();
     }
 protected:
     virtual const char* onGetName() {
diff --git a/gyp/core.gypi b/gyp/core.gypi
index 80b1939..b30a973 100644
--- a/gyp/core.gypi
+++ b/gyp/core.gypi
@@ -14,6 +14,7 @@
         '<(skia_src_path)/core/SkAdvancedTypefaceMetrics.cpp',
         '<(skia_src_path)/core/SkAlphaRuns.cpp',
         '<(skia_src_path)/core/SkAntiRun.h',
+        '<(skia_src_path)/core/SkBBoxHierarchy.cpp',
         '<(skia_src_path)/core/SkBBoxHierarchy.h',
         '<(skia_src_path)/core/SkBBoxRecord.cpp',
         '<(skia_src_path)/core/SkBBoxRecord.h',
diff --git a/src/core/SkBBoxHierarchy.cpp b/src/core/SkBBoxHierarchy.cpp
new file mode 100644
index 0000000..a99bb9d
--- /dev/null
+++ b/src/core/SkBBoxHierarchy.cpp
@@ -0,0 +1,12 @@
+

+/*

+ * Copyright 2012 Google Inc.

+ *

+ * Use of this source code is governed by a BSD-style license that can be

+ * found in the LICENSE file.

+ */

+

+#include "SkBBoxHierarchy.h"

+

+SK_DEFINE_INST_COUNT(SkBBoxHierarchy)

+

diff --git a/src/core/SkBBoxHierarchy.h b/src/core/SkBBoxHierarchy.h
index 347871f..6625a84 100644
--- a/src/core/SkBBoxHierarchy.h
+++ b/src/core/SkBBoxHierarchy.h
@@ -11,14 +11,15 @@
 
 #include "SkRect.h"
 #include "SkTDArray.h"
+#include "SkRefCnt.h"
 
 /**
  * Interface for a spatial data structure that associates user data pointers with axis-aligned
  * bounding boxes, and allows efficient retrieval of intersections with query rectangles.
  */
-class SkBBoxHierarchy {
+class SkBBoxHierarchy : public SkRefCnt {
 public:
-    virtual ~SkBBoxHierarchy() { }
+    SK_DECLARE_INST_COUNT(SkBBoxHierarchy)
 
     /**
      * Insert a data pointer and corresponding bounding box
@@ -47,6 +48,9 @@
      * Gets the number of insertions
      */
     virtual int getCount() const = 0;
+
+private:
+    typedef SkRefCnt INHERITED;
 };
 
 #endif
diff --git a/src/core/SkRTree.cpp b/src/core/SkRTree.cpp
index b6ff29b..96f6b18 100644
--- a/src/core/SkRTree.cpp
+++ b/src/core/SkRTree.cpp
@@ -19,6 +19,8 @@
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
+SK_DEFINE_INST_COUNT(SkRTree)
+
 SkRTree* SkRTree::Create(int minChildren, int maxChildren, SkScalar aspectRatio) {
     if (minChildren < maxChildren && (maxChildren + 1) / 2 >= minChildren &&
         minChildren > 0 && maxChildren < static_cast<int>(SK_MaxU16)) {
diff --git a/src/core/SkRTree.h b/src/core/SkRTree.h
index 9688159..1f16648 100644
--- a/src/core/SkRTree.h
+++ b/src/core/SkRTree.h
@@ -42,6 +42,7 @@
  */
 class SkRTree : public SkBBoxHierarchy {
 public:
+    SK_DECLARE_INST_COUNT(SkRTree)
 
     /**
      * Create a new R-Tree with specified min/max child counts.
@@ -175,6 +176,7 @@
 
     Node* allocateNode(uint16_t level);
 
+    typedef SkBBoxHierarchy INHERITED;
 };
 
 #endif
diff --git a/tests/RTreeTest.cpp b/tests/RTreeTest.cpp
index 61e6b67..d60bf0c 100644
--- a/tests/RTreeTest.cpp
+++ b/tests/RTreeTest.cpp
@@ -82,6 +82,7 @@
     DataRect rects[NUM_RECTS];
     SkRandom rand;
     SkRTree* rtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN);
+    SkAutoUnref au(rtree);
     REPORTER_ASSERT(reporter, NULL != rtree);
 
     int expectedDepthMin = -1;