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

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: Created 8 years, 7 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..b5cfbf0a9bf59883661038c379357b1dae764a61 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) {
awong 2012/06/08 17:33:15 * should associate with bool to stay consistent wi
mattm 2012/06/09 02:10:56 Done.
+ 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() {
+ ASSERT_TRUE(false);
awong 2012/06/08 17:33:15 How about: FAIL() << "Should never be called."
mattm 2012/06/09 02:10:56 Done.
+ }
+
+ Callback<void(void)> callback_;
awong 2012/06/08 17:33:15 Use Closure instead of Callback<void(void)>
mattm 2012/06/09 02:10:56 Done.
+ bool* deleted_;
+};
+
+TEST_F(CallbackTest, ResetRefCountedOwnerOfCallback) {
awong 2012/06/08 17:33:15 The name of the test doesn't quite express the hig
mattm 2012/06/09 02:10:56 Done.
+ 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