使用 QtWebEngine 退出时 QT 崩溃

Posted

技术标签:

【中文标题】使用 QtWebEngine 退出时 QT 崩溃【英文标题】:QT Crash on exit with QtWebEngine 【发布时间】:2021-09-10 07:50:32 【问题描述】:

所以我在我的 QML 中使用 WebEngineView,如下所示:

...
Loader 
    // some properties
    sourceComponent: WebEngineView 
        ...
    

在 c++ 逻辑中,我在构造函数和析构函数中使用 QQuickWebEngineProfile::defaultProfile()

MainViewModel(QObject* parent) : QObject(parent)

    // using QQuickWebEngineProfile::defaultProfile();
    // getting cookieStore of the profile and connect it to some slots


~MainViewModel()

    // using QQuickWebEngineProfile::defaultProfile();
    // getting cookieStore of the profile and disconnect it from MainViewModel

所以它工作得很好,但是当我试图关闭我的应用程序(调用 qApp->quit()) 时,它会崩溃。如果我从 QML 中删除 WebEngineView 它可以工作,如果我在 c++ 中删除使用 defaultProfile() 它可以工作。但我需要这些东西。

转储:

1  _threadid                                                                                                                                        ucrtbased                    0x7ffb48687c75 
2  _threadid                                                                                                                                        ucrtbased                    0x7ffb48687e13 
3  abort                                                                                                                                            ucrtbased                    0x7ffb4869e01d 
4  `anonymous namespace'::messageHandler                                                                                                            application.cpp         46   0x7ff7c2d2b1bf 
5  qt_message_print                                                                                                                                 qlogging.cpp            1844 0x7ffb08317fdf 
6  qt_message                                                                                                                                       qlogging.cpp            379  0x7ffb08318657 
7  QMessageLogger::fatal                                                                                                                            qlogging.cpp            890  0x7ffb08316612 
8  qt_assert                                                                                                                                        qglobal.cpp             3354 0x7ffb08307a48 
9  QAccessible::registerAccessibleInterface                                                                                                         qaccessible.cpp         747  0x7ffb01df22bd 
10 QAccessible::uniqueId                                                                                                                            qaccessible.cpp         767  0x7ffb01df2247 
11 QQuickWebEngineViewPrivate::widgetChanged                                                                                                        qquickwebengineview.cpp 982  0x7ffb4b2b5bda 
12 QQuickWebEngineViewPrivate::bindViewAndWidget                                                                                                    qquickwebengineview.cpp 972  0x7ffb4b2b5b6e 
13 QQuickWebEngineViewPrivate::releaseProfile                                                                                                       qquickwebengineview.cpp 200  0x7ffb4b2b2349 
14 QtWebEngineCore::ProfileAdapter::~ProfileAdapter                                                                                                 profile_adapter.cpp     127  0x7ffad4237f20 
15 QtWebEngineCore::ProfileAdapter::`vector deleting destructor'                                                                                    Qt5WebEngineCored            0x7ffad4238678 
16 std::default_delete<QtWebEngineCore::DevToolsFrontendQt::NetworkResourceLoader>::operator()                                                      memory                  1758 0x7ffad41d9a75 
17 std::unique_ptr<QtWebEngineCore::WebChannelIPCTransportHost,std::default_delete<QtWebEngineCore::WebChannelIPCTransportHost>>::reset             memory                  1910 0x7ffad4287857 
18 QtWebEngineCore::WebEngineContext::destroy                                                                                                       web_engine_context.cpp  339  0x7ffad4295a87 
19 QtWebEngineCore::WebEngineContext::destroyContextPostRoutine                                                                                     web_engine_context.cpp  425  0x7ffad4295bf8 
20 qt_call_post_routines                                                                                                                            qcoreapplication.cpp    336  0x7ffb087276ef 
21 QApplication::~QApplication                                                                                                                      qapplication.cpp        714  0x7ffb07767046 
  ...
30 main                                                                                                                                             main.cpp                63   0x7ff7c21d4640 

【问题讨论】:

似乎是一个未解决的问题:bugreports.qt.io/browse/QTBUG-90904 也许有什么解决方法? 如果我做对了,它会尝试通过它的 id 访问一个小部件,它是一个 nullptr。您可以尝试确保您的 id 不是 nullptr。 【参考方案1】:

它试图访问当时已被删除的主窗口。为了解决这个问题,我不得不延迟销毁我的主窗口。我已经完成了

QObject::deleteLater()

【讨论】:

以上是关于使用 QtWebEngine 退出时 QT 崩溃的主要内容,如果未能解决你的问题,请参考以下文章

安装程序在 ubuntu 上安装 qt 和 qt creator 时崩溃

Qt 应用程序在退出时崩溃

QtWebEngine 演示应用程序崩溃

编译 QtWebEngine 示例时找不到 c:\Users\qt\work\install\lib\Qt5Widgetsd.lib

在 QT5.5.1 示例中,FancyBrowser(基于 WebEngine)在退出时崩溃

Qt程序调试之Q_ASSERT断言(条件为真则跳过,否则直接异常+崩溃)