From ab97078faffa2d3321e721a887bb5fa3049dd513 Mon Sep 17 00:00:00 2001 From: Michal Klocek Date: Wed, 23 Aug 2017 12:32:18 +0200 Subject: Make deletion of CallbackSpy safe The deletion of CallBackSpy before web engine page destruction could end up in segmentation fault. Make CallbackSpy safe to be deleted before web engine page. Change-Id: I71a184091c0251c61e3383010e3badfef5cb6124 Reviewed-by: Viktor Engelmann --- .../auto/widgets/qwebenginepage/tst_qwebenginepage.cpp | 17 +++++++++++++++++ tests/auto/widgets/util.h | 14 +++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp index 4cb8e23e5..bea44f73c 100644 --- a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp +++ b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp @@ -105,6 +105,7 @@ private Q_SLOTS: void updatePositionDependentActionsCrash(); void createPluginWithPluginsEnabled(); void createPluginWithPluginsDisabled(); + void callbackSpyDeleted(); void destroyPlugin_data(); void destroyPlugin(); void createViewlessPlugin_data(); @@ -441,6 +442,22 @@ static QImage imageWithoutAlpha(const QImage &image) return result; } +void tst_QWebEnginePage::callbackSpyDeleted() +{ + QWebEnginePage *page = m_view->page(); + CallbackSpy spy; + QString poorManSleep("function wait(ms){" + " var start = new Date().getTime();" + " var end = start;" + " while (start + ms > end) {" + "end = new Date().getTime();" + " }" + "}" + "wait(1000);"); + page->runJavaScript(poorManSleep, spy.ref()); + //spy deleted before callback +} + void tst_QWebEnginePage::pasteImage() { // Pixels with an alpha value of 0 will have different RGB values after the diff --git a/tests/auto/widgets/util.h b/tests/auto/widgets/util.h index 356cf6ebb..ab3b9e6b9 100644 --- a/tests/auto/widgets/util.h +++ b/tests/auto/widgets/util.h @@ -67,15 +67,16 @@ public: }; template -struct RefWrapper { - R &ref; +struct CallbackWrapper { + QPointer p; void operator()(const T& result) { - ref(result); + if (p) + (*p)(result); } }; template -class CallbackSpy { +class CallbackSpy: public QObject { public: CallbackSpy() : called(false) { timeoutTimer.setSingleShot(true); @@ -100,10 +101,9 @@ public: eventLoop.quit(); } - // Cheap rip-off of boost/std::ref - RefWrapper > ref() + CallbackWrapper > ref() { - RefWrapper > wrapper = {*this}; + CallbackWrapper > wrapper = {this}; return wrapper; } -- cgit v1.2.3