Make sure _profile and _page are deleted in the correct order
authorChih-Hsuan Yen <yen@chyen.cc>
Wed, 19 Dec 2018 13:44:54 +0000 (21:44 +0800)
committerChih-Hsuan Yen <yen@chyen.cc>
Wed, 19 Dec 2018 13:44:54 +0000 (21:44 +0800)
Inspired by https://github.com/electron/electron/pull/15028

Closes https://github.com/nextcloud/desktop/issues/941
Closes https://github.com/nextcloud/desktop/issues/950

src/gui/creds/webflowcredentialsdialog.cpp
src/gui/creds/webflowcredentialsdialog.h
src/gui/wizard/webview.cpp
src/gui/wizard/webview.h

index 85dd874515096d66ca65fd4140d9370f123a9d56..2d22ba06e3a1c54bf566fe66b26d46f7e63b562c 100644 (file)
@@ -29,6 +29,14 @@ WebFlowCredentialsDialog::WebFlowCredentialsDialog(QWidget *parent)
     connect(_webView, &WebView::urlCatched, this, &WebFlowCredentialsDialog::urlCatched);
 }
 
+void WebFlowCredentialsDialog::closeEvent(QCloseEvent* e) {
+    Q_UNUSED(e);
+
+    // Force calling WebView::~WebView() earlier so that _profile and _page are
+    // deleted in the correct order.
+    delete _webView;
+}
+
 void WebFlowCredentialsDialog::setUrl(const QUrl &url) {
     _webView->setUrl(url);
 }
index 84ed4ea12e2a702887bcece1eba18935beb772be..9849ee3a46c64bb787c1f802008e952f36e425c0 100644 (file)
@@ -21,6 +21,9 @@ public:
     void setInfo(const QString &msg);
     void setError(const QString &error);
 
+protected:
+    void closeEvent(QCloseEvent * e) override;
+
 signals:
     void urlCatched(const QString user, const QString pass, const QString host);
 
index 1c6a7436389424782d4ac3bcb78b5ec01ed539af..929b61205188e32ae10d0358fb4f33fc590a19b3 100644 (file)
@@ -105,6 +105,19 @@ void WebView::setUrl(const QUrl &url) {
     _page->setUrl(url);
 }
 
+WebView::~WebView() {
+    /*
+     * The Qt implmentation deletes children in the order they are added to the
+     * object tree, so in this case _page is deleted after _profile, which
+     * violates the assumption that _profile should exist longer than
+     * _page [1]. Here I delete _page manually so that _profile can be safely
+     * deleted later.
+     *
+     * [1] https://doc.qt.io/qt-5/qwebenginepage.html#QWebEnginePage-1
+     */
+    delete _page;
+}
+
 WebViewPageUrlRequestInterceptor::WebViewPageUrlRequestInterceptor(QObject *parent)
     : QWebEngineUrlRequestInterceptor(parent) {
 
index e0b93ec8497113b18643925350b3fcc4abd3b4c5..93baf6b59faa0b9d7f38236902400dc9517ce4a6 100644 (file)
@@ -21,6 +21,7 @@ class WebView : public QWidget
     Q_OBJECT
 public:
     WebView(QWidget *parent = nullptr);
+    virtual ~WebView();
     void setUrl(const QUrl &url);
 
 signals: