test: event-thread: Destroy Object from correct thread context
The EventHandler used in the test is destroyed from the main thread, which is invalid for a thread-bound object bound to a different thread. Fix it by destroying it with deleteLater(). Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Milan Zamazal <mzamazal@redhat.com>
This commit is contained in:
parent
f422624d9c
commit
1f5d485bfb
1 changed files with 27 additions and 10 deletions
|
@ -85,10 +85,17 @@ private:
|
||||||
class EventThreadTest : public Test
|
class EventThreadTest : public Test
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
|
int init()
|
||||||
|
{
|
||||||
|
thread_.start();
|
||||||
|
|
||||||
|
handler_ = new EventHandler();
|
||||||
|
|
||||||
|
return TestPass;
|
||||||
|
}
|
||||||
|
|
||||||
int run()
|
int run()
|
||||||
{
|
{
|
||||||
Thread thread;
|
|
||||||
thread.start();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Fire the event notifier and then move the notifier to a
|
* Fire the event notifier and then move the notifier to a
|
||||||
|
@ -98,23 +105,33 @@ protected:
|
||||||
* different thread will correctly process already pending
|
* different thread will correctly process already pending
|
||||||
* events in the new thread.
|
* events in the new thread.
|
||||||
*/
|
*/
|
||||||
EventHandler handler;
|
handler_->notify();
|
||||||
handler.notify();
|
handler_->moveToThread(&thread_);
|
||||||
handler.moveToThread(&thread);
|
|
||||||
|
|
||||||
this_thread::sleep_for(chrono::milliseconds(100));
|
this_thread::sleep_for(chrono::milliseconds(100));
|
||||||
|
|
||||||
/* Must stop thread before destroying the handler. */
|
if (!handler_->notified()) {
|
||||||
thread.exit(0);
|
|
||||||
thread.wait();
|
|
||||||
|
|
||||||
if (!handler.notified()) {
|
|
||||||
cout << "Thread event handling test failed" << endl;
|
cout << "Thread event handling test failed" << endl;
|
||||||
return TestFail;
|
return TestFail;
|
||||||
}
|
}
|
||||||
|
|
||||||
return TestPass;
|
return TestPass;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void cleanup()
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Object class instances must be destroyed from the thread
|
||||||
|
* they live in.
|
||||||
|
*/
|
||||||
|
handler_->deleteLater();
|
||||||
|
thread_.exit(0);
|
||||||
|
thread_.wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
EventHandler *handler_;
|
||||||
|
Thread thread_;
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_REGISTER(EventThreadTest)
|
TEST_REGISTER(EventThreadTest)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue