Search code examples
c++qtqtwebengineqt5.9qt5.12

QWebEngine: deleting QWebEngineUrlRequestInterceptor results in crash


in my project I want to create/delete QWebEnginePages when opening/closing an app. With Qt5.9.7 everything worked fine. While switching to Qt5.12.6 my application will crash.

I created a small code example, where I just click a button for creating and deleting. Of course in my real project I make sure that everything is guarded etc:

BrowserWidget::BrowserWidget(QWidget *parent)
    : QWidget(parent), ui(new Ui::Widget) {
  m_webView = new QWebEngineView(this);

  ui->setupUi(this);
  ui->verticalLayout->addWidget(m_webView);

  connect(ui->createBtn, &QPushButton::clicked, this,
          &BrowserWidget::createSession);
  connect(ui->deleteBtn, &QPushButton::clicked, this,
          &BrowserWidget::deleteSession);
}

BrowserWidget::~BrowserWidget() {
  delete ui;
}

void BrowserWidget::createSession() {
  m_page = new QWebEnginePage(this);
  m_interceptor = new UrlRequestInterceptor(this);
  m_page->profile()->setRequestInterceptor(m_interceptor);

  for (auto c : m_connections) {
    disconnect(c);
  }
  m_connections.clear();

  m_page->setUrl(
      QUrl(QStringLiteral("https://www.youtube.com/watch?v=rNSnfXl1ZjU")));
  m_webView->setPage(m_page);

  return;
}

void BrowserWidget::deleteSession() {
  delete m_page;
  delete m_interceptor; // This causes the crash
}

The whole UrlRequestInterceptor.cpp looks like this:

UrlRequestInterceptor::UrlRequestInterceptor(QObject *parent)
    : QWebEngineUrlRequestInterceptor(parent) {}

void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) {
  info.block(false);
  return;
}

When I fast open and close my "WebPages", the delete m_interceptor will lead to a crash. This also happened sometimes when the webpage was completely loaded, but not that often. I was not able to find anything about it in the documentation.

Does somebody of you have any idea how I can reliably wait for all interceptor/browser-related stuff to be finished before I delete the m_interceptor?

Here is the stack trace if it is usefull:

1   QtWebEngineCore::CustomProtocolHandler::MaybeCreateJob          Qt5WebEngineCored  0x7ffd06bdd708 
2   QtWebEngineCore::CookieMonsterDelegateQt::hasCookieMonster      Qt5WebEngineCored  0x7ffd0a4aa0ae 
3   QtWebEngineCore::CookieMonsterDelegateQt::hasCookieMonster      Qt5WebEngineCored  0x7ffd0a4b1ca5 
4   QtWebEngineCore::WebContentsAdapter::webContents                Qt5WebEngineCored  0x7ffd094b5a12 
5   QtWebEngineCore::WebContentsAdapter::webContents                Qt5WebEngineCored  0x7ffd094b4a01 
6   QtWebEngineCore::WebContentsAdapter::webContents                Qt5WebEngineCored  0x7ffd094b0769 
7   QtWebEngineCore::WebContentsAdapter::webContents                Qt5WebEngineCored  0x7ffd094b5777 
8   QtWebEngineCore::WebContentsAdapter::webContents                Qt5WebEngineCored  0x7ffd094a83c8 
9   QtWebEngineCore::WebContentsAdapter::webContents                Qt5WebEngineCored  0x7ffd094a078f 
10  QtWebEngineCore::WebContentsAdapter::webContents                Qt5WebEngineCored  0x7ffd094a34ca 
11  QtWebEngineCore::WebContentsAdapter::webContents                Qt5WebEngineCored  0x7ffd0949ffdf 
12  QtWebEngineCore::WebContentsAdapter::webContents                Qt5WebEngineCored  0x7ffd094a65c5 
13  QtWebEngineCore::WebContentsAdapter::webContents                Qt5WebEngineCored  0x7ffd094a67af 
14  QtWebEngineCore::WebContentsAdapter::webContents                Qt5WebEngineCored  0x7ffd094c15d3 
15  QWebEngineCookieStorePrivate::q_func                            Qt5WebEngineCored  0x7ffd0b93f07d 
16  QtWebEngineCore::WebContentsAdapter::webContents                Qt5WebEngineCored  0x7ffd094b91bb 
17  QtWebEngineCore::JavaScriptDialogController::qt_static_metacall Qt5WebEngineCored  0x7ffd06db6c45 
18  QWebEngineQuotaRequest::QWebEngineQuotaRequest                  Qt5WebEngineCored  0x7ffd08ea7a85 
19  QtWebEngineCore::FilePickerController::mode                     Qt5WebEngineCored  0x7ffd0a1c5e75 
20  QtWebEngineCore::FilePickerController::mode                     Qt5WebEngineCored  0x7ffd0a1c4a31    

Thank you very much!


Solution

  • I think you don't really need to create a new interceptor instance every time a session is started. Just have one instance of it, in your BrowserWidget class:

    private:
        Ui::BrowserWidget *ui;
        UrlRequestInterceptor m_interceptor;
    

    initialize it in the widget constructor:

    BrowserWidget::BrowserWidget(QWidget *parent)
        : QWidget(parent), ui(new Ui::BrowserWidget), m_interceptor(this)
    

    pass a pointer to it to the page profile:

    void BrowserWidget::createSession() {
      m_page = new QWebEnginePage(this);
      m_page->profile()->setRequestInterceptor(&m_interceptor);
    

    get rid of the delete in deleteSession:

    void BrowserWidget::deleteSession() {
    
      delete m_page;
    //  delete m_interceptor; // This (no more) causes the crash
    }