Dear All,
I want to @ the threading owner,
j...@chromium.orgRefer to base/threading codes,
message_loop_ was created in Thread::StartWithOptions function and destructed in Thread::ThreadMain
bool Thread::StartWithOptions(const Options& options) {
scoped_ptr<MessageLoop> message_loop = MessageLoop::CreateUnbound(
type, options.message_pump_factory);
message_loop_ = message_loop.get();
}
As I know, these two function are running in different threads.
a object was created in A thread, but was destroyed in B thread, it is not a good coding style.
There will be potential issue when try to destroy an thread object.
Thread::~Thread() {
Stop();
}
If the sequence running like:
1. cpu run in io thread, call XXXThread::Stop, check message_loop_ has valid value, then goes to task_runner function
2. cpu switch to XXXThread thread, it goes to the end of its ThreadMain, and set message_loop_ to null
3. cpu switch to io thread again, and execute
message_loop_->task_runner(), at that time, message_loop_ was null, then it will cause the segment fault issue.
So I want to ask is it designed to handle message_loop_ so in chromium ?
If not, I want to commit a patch to make message_loop_ create and destruct in same thread like :
diff --git a/base/threading/thread.cc b/base/threading/thread.cc
index cf7e313..d8ea35b 100755
--- a/base/threading/thread.cc
+++ b/base/threading/thread.cc
@@ -71,7 +71,11 @@ Thread::Thread(const std::string& name)
}
Thread::~Thread() {
+ scoped_ptr<MessageLoop> message_loop(message_loop_);
Stop();
+ // We can't receive messages anymore.
+ // (The message loop is destructed at the end of this block)
+ message_loop_ = nullptr;
}
bool Thread::Start() {
@@ -230,7 +234,6 @@ void Thread::ThreadMain() {
// Lazily initialize the message_loop so that it can run on this thread.
DCHECK(message_loop_);
- scoped_ptr<MessageLoop> message_loop(message_loop_);
message_loop_->BindToCurrentThread();
message_loop_->set_thread_name(name_);
message_loop_->SetTimerSlack(message_loop_timer_slack_);
@@ -270,10 +273,6 @@ void Thread::ThreadMain() {
// Assert that MessageLoop::Quit was called by ThreadQuitHelper.
DCHECK(GetThreadWasQuitProperly());
-
- // We can't receive messages anymore.
- // (The message loop is destructed at the end of this block)
- message_loop_ = nullptr;
}
Best Regards
Errong Leng