Qt World Summit: Submit your Presentation

How to Terminate QtWebEngine Before Exiting

  • Context: It is very important for our system to be memory-leak free. We build Qt, linking to mfc to ensure a common heap is used. This allows us to work with the _CRTDBG_MAP_ALLOC/_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ) calls to detect memory leaking on exit.

    We've recently added a QtWebEngineView as a widget to our system, which brings along all the web engine processes and threads. On exit, it appears that these threads aren't shut down, and all memory resources are (erroneously?) reported as leaks. I've read through the documentation, and I'm struggling to find something to "correctly" terminate and release all web engine content prior to exit. A similar issue happens when working just with an OpenGL widget.

    Question: How does one correctly terminate and release all resources - including its OpenGL context - associated with qt web engine?

    If the motivation is insufficient, another justification would be how to release all web engine resources during program execution, in the case of limited resources?

    A simple example program:

    #include <QtCore>
    #include <QtWidgets\QMainWindow>
    #include <QtWebEngine\QtWebEngine>
    #include <QtWebEngineWidgets>
    #define _CRTDBG_MAP_ALLOC
    int main( int argc, char * argv[] )
    	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); // enable memory leak checking
    	int result = 0;
    		QApplication app( argc, argv );
    		QtWebEngine::initialize(); // reports leaks - likely not closed correctly on exit
    		QMainWindow mainWindow;
    		QWebEngineView *webView = new QWebEngineView; // more leaks...
    		webView->load( QUrl( "https://www.qt.io" ) ); // even more leaks...
    		mainWindow.setCentralWidget( webView );
    		mainWindow.resize( 800, 600 );
    		result = app.exec();
    	// expected Qt to have finished offloading here
    	return result;

  • Hi! Your code looks fine to me and my guess would be that this is a known issue. I don't think anyone here can help you with this, so better ask the devs on the mailing list.

  • Thanks Wieland for taking a look. And thanks for the mailing list suggestion! Good to know of more Qt resources.

    I've filed a bug: https://bugreports.qt.io/browse/QTBUG-56774