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

Unified Diff: sql/connection_unittest.cc

Issue 1923983004: Example of inappropriate cross-thread weak-ptr use. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sql/connection_unittest.cc
diff --git a/sql/connection_unittest.cc b/sql/connection_unittest.cc
index 1c08881227d5304fc2a74a4e13ec788289809983..ef044ac8d18148022f389029e4bbeb908d33b7bb 100644
--- a/sql/connection_unittest.cc
+++ b/sql/connection_unittest.cc
@@ -12,7 +12,10 @@
#include "base/logging.h"
#include "base/macros.h"
#include "base/metrics/statistics_recorder.h"
+#include "base/single_thread_task_runner.h"
#include "base/test/histogram_tester.h"
+#include "base/thread_task_runner_handle.h"
+#include "base/threading/thread.h"
#include "base/trace_event/process_memory_dump.h"
#include "sql/connection.h"
#include "sql/connection_memory_dump_provider.h"
@@ -1488,4 +1491,71 @@ TEST_F(SQLConnectionTest, GetAppropriateMmapSize) {
}
#endif
+namespace {
+
+class Demo {
+ public:
+ explicit Demo(scoped_refptr<base::SingleThreadTaskRunner> task_runner)
+ : background_task_runner_(task_runner),
+ weak_ptr_factory_(this) {
+ }
+ ~Demo() {
+ }
+
+ void Ping(
+ scoped_refptr<base::SingleThreadTaskRunner> callback_runner,
+ const base::Closure& callback) {
+ LOG(ERROR) << "Received ping, sleeping";
+ base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
+ LOG(ERROR) << "Sending pong";
+ callback_runner->PostTask(FROM_HERE, callback);
+ }
+
+ void SendPing() {
+ // This use of weak pointer is valid, because it will be
+ // dereferenced when the task is posted back on the current
+ // thread.
+ base::Closure callback(base::Bind(&Demo::Pong,
+ weak_ptr_factory_.GetWeakPtr()));
+
+ // This use of weak pointer is not valid, because it will be
+ // dereferenced when the task is posted on the background thread.
+ // It fails the sequence_checker_.CalledOnValidSequencedThread()
+ // DCHECK in WeakReference::Flag::IsValid().
+ background_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&Demo::Ping, weak_ptr_factory_.GetWeakPtr(),
+ base::ThreadTaskRunnerHandle::Get(), callback));
+ }
+
+ void Pong() {
+ LOG(ERROR) << "Received pong, sleeping";
+ base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
+ LOG(ERROR) << "Sending ping";
+ SendPing();
+ }
+
+ void Start() {
+ LOG(ERROR) << "Sending initial ping";
+ SendPing();
+ }
+
+ private:
+ scoped_refptr<base::SingleThreadTaskRunner> background_task_runner_;
+
+ base::WeakPtrFactory<Demo> weak_ptr_factory_;
+};
+
+} // namespace
+
+TEST_F(SQLConnectionTest, WeakDemo) {
+ base::MessageLoop message_loop;
+
+ base::Thread background_thread("demo");
+ ASSERT_TRUE(background_thread.Start());
+
+ Demo d(background_thread.task_runner());
+ d.Start();
+}
+
} // namespace sql
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698