QWebView 内存泄漏 [英] Memory leak in QWebView
问题描述
我正在使用 Qt5.4.3,在某些时候,Qt 会打印出这样的消息
I am using Qt5.4.3 and at some point, Qt prints out message like this
LEAK: 145 CachedResource
LEAK: 4432 WebCoreNode
我尝试在 Qt 应用程序上显示网页,但无论网页是什么,内存总是泄漏.
I try to display a webpage on a Qt application, but whatever the webpage is, memory always leaks.
这里是名为test"的项目的所有代码(我在mainwindow.ui中添加了webview控件)
Here is the all code of project named"test"(I add the webview control in mainwindow.ui)
#test.pro
QT += core gui webkit
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets webkitwidgets multimedia multimediawidgets
TARGET = EchartDemo
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
FORMS += widget.ui
INCLUDEPATH += $$PWD
MOC_DIR = temp/moc
RCC_DIR = temp/rcc
UI_DIR = temp/ui
OBJECTS_DIR = temp/obj
DESTDIR = bin
//main.cpp
#include "widget.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.showMaximized();
return a.exec();
}
//widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
//`widget.cpp`
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->webView->load(QUrl("file:///"+qApp->applicationDirPath()+"/html/hao.html"));
}
Widget::~Widget()
{
delete ui;
}
//hao.html
<meta http-equiv="Refresh" content="0; url=http://www.hao123.com/?1460255739"/><meta property="shurufa:url-navigate" content="985" />
为了正常显示网页,需要将html文件放到\build-EchartDemo-Desktop_Qt_5_4_2_MinGW_32bit-Debug\bin\html
文件夹下.
In order to show the webpage normally, you should put the html file to the folder \build-EchartDemo-Desktop_Qt_5_4_2_MinGW_32bit-Debug\bin\html
.
当然,您可以将 hao.html
文件的内容更改为您喜欢的任何内容.
Of course, you can change the content of the hao.html
file to whatever you like.
为什么我会出现这些内存泄漏?
Why do I have these memory leaks?
推荐答案
这是 Qt 中的一个已知错误,这些内存泄漏"只是某些情况下的警告.你可以在这里阅读更多相关信息:Qt Bug40373,并查看提及这些泄漏的其他错误报告.
This is a known bug in Qt and these "memory leaks" are only warnings in some cases. You can read more about it here : Qt Bug 40373, and also see the other bug reports mentioning those leaks.
此外,您应该考虑使用 QWebEngineView ,这要好得多;)(并更新到 Qt5.6,但这是另一回事!).
Also, you should consider to use QWebEngineView which is much better ;) (and update to Qt5.6, but it is another story!).
这篇关于QWebView 内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!