PDA

View Full Version : How to Terminate QtWebEngine Before Exiting



disckitty
27th October 2016, 16:51
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 );
mainWindow.show();


result = app.exec();
}
// expected Qt to have finished offloading here

return result;
}

anda_skoa
27th October 2016, 19:36
If you look at the code for QWebEngine::initialize() (https://code.woboq.org/qt5/qtwebengine/src/core/api/qtwebenginecoreglobal.cpp.html) you see that it registers a cleanup hook via qAddPostRoutine() (http://doc.qt.io/qt-5/qcoreapplication.html#qAddPostRoutine).
These hooks are executes by the application object's destructor.

If the end-of-scope destruction in main() is too late for you, then you will probably have to create the application object on the heap and delete it before main() ends.

Cheers,
_

disckitty
27th October 2016, 23:58
Thanks anda_skoa for the suggestion. unfortunately while it deletes the application there are still resources around. I've logged a bug: https://bugreports.qt.io/browse/QTBUG-56774 and stepping my way through the Qt code to see if there's a problem.

d_stranz
29th October 2016, 17:05
We have found that when using MFC-based DLLs in a Qt app there are extensive apparent memory leaks reported on application shutdown when running in the Visual Studio debugger. I say "apparent" because this has been reported as a known issue resulting from DLLs being unloaded in a different order than they were loaded. Here's a discussion on stackoverflow (http://stackoverflow.com/questions/1008514/on-closing-a-qt-4-5-application-visual-studio-reports-that-it-has-detected-memo). The last comment may offer a solution.