summaryrefslogtreecommitdiffstats
path: root/base/memory
diff options
context:
space:
mode:
authorsergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-05 03:51:10 +0000
committersergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-05 03:51:10 +0000
commite9b738ea96bb9745c9ed4fe49daa707d0d874c35 (patch)
tree9e6435d21eec15173be463d2472082c6a29066ed /base/memory
parent1674649497e1a2f646741b7cb0239113d1f7111a (diff)
downloadchromium_src-e9b738ea96bb9745c9ed4fe49daa707d0d874c35.zip
chromium_src-e9b738ea96bb9745c9ed4fe49daa707d0d874c35.tar.gz
chromium_src-e9b738ea96bb9745c9ed4fe49daa707d0d874c35.tar.bz2
Make sure that scoped_ptr<> cannot be used with ref-counted objects.
It's easy to type scoped_ptr<> instead of scoped_refptr<>. Such bugs are hard to debug sometimes. This change adds compile-time check to make sure that scoped_ptr<> is not used for ref-counted objects. Also fixed one (benign) instance of scoped_ptr<> being used for a ref-counted object. Review URL: http://codereview.chromium.org/9958150 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@130835 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/memory')
-rw-r--r--base/memory/scoped_ptr.h25
-rw-r--r--base/memory/scoped_ptr_unittest.nc11
2 files changed, 36 insertions, 0 deletions
diff --git a/base/memory/scoped_ptr.h b/base/memory/scoped_ptr.h
index 77a2039..bc38c8d 100644
--- a/base/memory/scoped_ptr.h
+++ b/base/memory/scoped_ptr.h
@@ -96,8 +96,30 @@
#include <stddef.h>
#include <stdlib.h>
+#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/move.h"
+#include "base/template_util.h"
+
+namespace base {
+
+namespace subtle {
+class RefCountedBase;
+class RefCountedThreadSafeBase;
+} // namespace subtle
+
+namespace internal {
+
+template <typename T> struct IsNotRefCounted {
+ enum {
+ value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value &&
+ !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>::
+ value
+ };
+};
+
+} // namespace internal
+} // namespace base
// A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
// automatically deletes the pointer it holds (if any).
@@ -112,6 +134,9 @@ template <class C>
class scoped_ptr {
MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue)
+ COMPILE_ASSERT(base::internal::IsNotRefCounted<C>::value,
+ C_is_refcounted_type_and_needs_scoped_refptr);
+
public:
// The element type
diff --git a/base/memory/scoped_ptr_unittest.nc b/base/memory/scoped_ptr_unittest.nc
index 841d03c..30a332e 100644
--- a/base/memory/scoped_ptr_unittest.nc
+++ b/base/memory/scoped_ptr_unittest.nc
@@ -4,6 +4,7 @@
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
+#include "base/memory/ref_counted.h"
namespace {
@@ -13,6 +14,9 @@ class Parent {
class Child : public Parent {
};
+class RefCountedClass : public base::RefCountedThreadSafe<RefCountedClass> {
+};
+
} // namespace
#if defined(NCTEST_NO_PASSAS_DOWNCAST) // [r"invalid conversion from"]
@@ -21,4 +25,11 @@ scoped_ptr<Child> DowncastUsingPassAs(scoped_ptr<Parent> object) {
return object.PassAs<Child>();
}
+#elif defined(NCTEST_NO_REF_COUNTED_SCOPED_PTR) // [r"creating array with negative size"]
+
+// scoped_ptr<> should not work for ref-counted objects.
+void WontCompile() {
+ scoped_ptr<RefCountedClass> x;
+}
+
#endif