Jack
Jack

Reputation: 16724

How to properly close QWebView without result in memory leak?

I have a custom QWebView declared as:

class browserControl : public QWebView {
  Q_OBJECT
 public:
         explicit browserControl(QWidget *parent = 0);
         ~browserControl();
  // ....
}

it's a member of MainWindow class (the same one generated by Qt on GUI applications) I call show() when a button is hit. So it open the web browser in a new windows but when I close either the main window our the web browser it I got a bunch of memory leaks (it may differ from each run, depending on what the page opened, I guess) like (if I close main windows then browser)

LEAK: 24 CachedResource

LEAK: 231 WebCoreNode

Or if I close the browser then main window:

LEAK: 1 XMLHttpRequest

LEAK: 49 CachedResource

LEAK: 2528 WebCoreNode

How do I fix this?

EDIT

The constructor code as asked:

browserControl::browserControl(QWidget *parent)
    : QWebView(parent)
{
}

browserControl::~browserControl()
{
}

Upvotes: 0

Views: 1188

Answers (1)

Mehrdad
Mehrdad

Reputation: 1256

You have two options:

  1. You can pass a parent QObject, which is directly or indirectly a child of your QApplication object, to constructor of the widget. Since destructor of qobject delete its children, eventually your widget will be deleted.

  2. Second option is that you set Qt::WA_DeleteOnClose attribute on your widget, this way it will be deleted when you close the widget itself, and won't be waiting for the application to be closed. do this: widget->setAttribute(Qt::WA_DeleteOnClose);

Upvotes: 2

Related Questions