在 Qt 5.0.2 上打开 QWebView 时发生内存泄漏问题。我唯一要做的就是用一个简单的本地网页打开一个 QWebWindow。该脚本通过执行 ajax 请求来调用服务器并无限期地重复该操作。缓存、localStorage 或 sessionStorage 中没有保存数据。问题是即使运行一个小时后应用程序也会占用太多内存。(超过 300Mb)。
我不确定这是与 qt 相关的问题还是与 javascript 相关的问题。
这是我使用的代码:
#include <QWebFrame>
#include <QWebElementCollection>
#include <QNetworkDiskCache>
#include <QDesktopWidget>
#include <QWebHistory>
#include "mainwindow.h"
MainWin::MainWin(QWidget * parent) : QWebView(parent)
{
m_network = new QNetworkAccessManager(this);
m_cache = new QNetworkDiskCache(this);
m_cache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/test");
m_cache->setMaximumCacheSize(0);
m_network->setCache(m_cache);
page()->setNetworkAccessManager(m_network);
page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true); // enable inspector
// local content can access remote urls
page()->settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);
// enable javascript
page()->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
// Plug-ins must be set to be enabled to use plug-ins.
page()->settings()->setAttribute(QWebSettings::PluginsEnabled,true);
// Images are automatically loaded in web pages.
page()->settings()->setAttribute(QWebSettings::AutoLoadImages,true);
page()->settings()->setMaximumPagesInCache(0);
page()->settings()->setObjectCacheCapacities(0,0,0); // causing slight flicker on refresh
page()->settings()->clearMemoryCaches();
page()->history()->setMaximumItemCount(0);
// enable local storage
page()->settings()->enablePersistentStorage("C:\\data\\");
// hide the vertical scrollbar
page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
// Signal is emitted before frame loads any web content:
QObject::connect(page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
this, SLOT(addJSObject()));
setUrl(startURL);
initEffects();
}
void MainWin::addJSObject() {
}
/*
* EFFECTS
*/
void MainWin::initEffects() {
resize(500, 300);
move(0, 0); // left top corner
}
void MainWin::resize(int width, int height) {
setGeometry(QRect(this->x(), this->y() - (height - this->height()), width, height));
}
的HTML:
<html lang="en">
<head>
<title>Details</title>
<script type="text/javascript" src="./js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="./js/connection.js"></script>
</head>
<body>
<div>page loaded: <span id="times">0</span> times</div>
</body>
</html>
和Javascript:
jQuery(document).ready(function($) {
var counter = 0;
var refreshData;
refreshData = function() {
counter++;
var jData = '{"test":"test"}';
var request = $.ajax({
type: "POST",
url: "http://qt.digia.com/",
dataType: "json",
data: { jsonData: jData },
timeout: 15000, // 15 secs
complete: function(response) {
console.log(response);
delete response;
$('#times').html(counter);
// no need to hide the window, usual behavior
refreshDataTimeout = setTimeout(refreshData, 1000);
}
});
request.onreadystatechange = null;
request.abort = null;
request = null;
delete request;
}
refreshData();
});