Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3095)

Unified Diff: base/callback_unittest.cc

Issue 10388060: Make Callback.Reset safe against deleting itself. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review changes Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/callback_internal.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/callback_unittest.cc
diff --git a/base/callback_unittest.cc b/base/callback_unittest.cc
index f0f3915a2cb8ce2e5c84e7c9ce13bd56a3839d78..2c2bef1a13748f11d300953a9e6e99f62f5c27ca 100644
--- a/base/callback_unittest.cc
+++ b/base/callback_unittest.cc
@@ -6,6 +6,7 @@
#include "base/callback.h"
#include "base/callback_helpers.h"
#include "base/callback_internal.h"
+#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -145,5 +146,36 @@ TEST_F(CallbackTest, ResetAndReturn) {
ASSERT_TRUE(tfr.cb_already_run);
}
+class CallbackOwner : public base::RefCounted<CallbackOwner> {
+ public:
+ CallbackOwner(bool* deleted) {
+ callback_ = Bind(&CallbackOwner::Unused, this);
+ deleted_ = deleted;
+ }
+ void Reset() {
+ callback_.Reset();
+ // We are deleted here if no-one else had a ref to us.
+ }
+
+ private:
+ friend class base::RefCounted<CallbackOwner>;
+ virtual ~CallbackOwner() {
+ *deleted_ = true;
+ }
+ void Unused() {
+ FAIL() << "Should never be called";
+ }
+
+ Closure callback_;
+ bool* deleted_;
+};
+
+TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) {
+ bool deleted = false;
+ CallbackOwner* owner = new CallbackOwner(&deleted);
+ owner->Reset();
+ ASSERT_TRUE(deleted);
+}
+
} // namespace
} // namespace base
« no previous file with comments | « base/callback_internal.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698