fix: fixed several other memory leaks

This commit is contained in:
Qv2ray-dev 2020-02-09 10:38:46 +08:00
parent 8938d505ca
commit e463ccc22f
16 changed files with 271 additions and 253 deletions

View File

@ -1 +1 @@
3801
3803

View File

@ -18,6 +18,12 @@ namespace Qv2ray::common
return randomString;
}
QString StringFromFile(const QString &filePath)
{
QFile f(filePath);
return StringFromFile(&f);
}
QString StringFromFile(QFile *source)
{
source->open(QFile::ReadOnly);

View File

@ -19,6 +19,7 @@ namespace Qv2ray::common
void QvMessageBoxInfo(QWidget *parent, QString title, QString text);
QMessageBox::StandardButton QvMessageBoxAsk(QWidget *parent, QString title, QString text, QMessageBox::StandardButton extraButtons = QMessageBox::NoButton);
//
QString StringFromFile(const QString &filePath);
QString StringFromFile(QFile *source);
bool StringToFile(const QString *text, QFile *target);
//

View File

@ -38,7 +38,7 @@ namespace Qv2ray::components::pac
//
DEBUG(PROXY, "PAC Listening local endpoint: " + address + ":" + QSTRN(port))
//
QString gfwContent = StringFromFile(std::make_unique<QFile>(QV2RAY_RULES_GFWLIST_PATH).get());
QString gfwContent = StringFromFile(QV2RAY_RULES_GFWLIST_PATH);
pacContent = ConvertGFWToPAC(gfwContent, proxyString);
//
auto result = pacServer->listen(QHostAddress(address), static_cast<ushort>(port));

View File

@ -60,6 +60,7 @@ namespace Qv2ray::components::plugins::Toolbar
}
server->close();
delete server;
}
void StartMessageQThread()
{

View File

@ -7,7 +7,7 @@ namespace Qv2ray::core::connection
{
CONFIGROOT _ReadConnection(const QString &connection)
{
QString jsonString = StringFromFile(std::make_unique<QFile>(connection).get());
QString jsonString = StringFromFile(connection);
auto conf = CONFIGROOT(JsonFromString(jsonString));
if (conf.count() == 0) {
@ -71,17 +71,18 @@ namespace Qv2ray::core::connection
bool SaveConnectionConfig(CONFIGROOT obj, QString *alias, bool canOverrideExisting)
{
auto str = JsonToString(obj);
QFile *config = new QFile(QV2RAY_CONFIG_DIR + *alias + QV2RAY_CONFIG_FILE_EXTENSION);
auto fullPath = QV2RAY_CONFIG_DIR + *alias + QV2RAY_CONFIG_FILE_EXTENSION;
// If there's already a file AND we CANNOT override existing file.
if (config->exists() && !canOverrideExisting) {
if (QFile::exists(fullPath) && !canOverrideExisting) {
// Alias is a pointer to a QString.
DeducePossibleFileName(QV2RAY_CONFIG_DIR, alias, QV2RAY_CONFIG_FILE_EXTENSION);
config = new QFile(QV2RAY_CONFIG_DIR + *alias + QV2RAY_CONFIG_FILE_EXTENSION);
fullPath = QV2RAY_CONFIG_DIR + *alias + QV2RAY_CONFIG_FILE_EXTENSION;
}
LOG(SETTINGS, "Saving a config named: " + *alias)
return StringToFile(&str, config);
QFile config(fullPath);
return StringToFile(&str, &config);
}
bool SaveSubscriptionConfig(CONFIGROOT obj, const QString &subscription, QString *name)
@ -93,15 +94,15 @@ namespace Qv2ray::core::connection
fName = RemoveInvalidFileName(fName);
}
QFile *config = new QFile(QV2RAY_SUBSCRIPTION_DIR + subscription + "/" + fName + QV2RAY_CONFIG_FILE_EXTENSION);
QFile config(QV2RAY_SUBSCRIPTION_DIR + subscription + "/" + fName + QV2RAY_CONFIG_FILE_EXTENSION);
// If there's already a file. THIS IS EXTREMELY RARE
if (config->exists()) {
if (config.exists()) {
LOG(FILEIO, "Trying to overrwrite an existing subscription config file. THIS IS RARE")
}
LOG(SETTINGS, "Saving a subscription named: " + fName)
bool result = StringToFile(&str, config);
bool result = StringToFile(&str, &config);
if (!result) {
LOG(FILEIO, "Failed to save a connection config from subscription: " + subscription + ", name: " + fName)

View File

@ -52,7 +52,7 @@ namespace Qv2ray::core::kernel::api
thread->wait();
// Although thread shouldnot be null, we'll add this check to be safe.
if (thread != nullptr) {
if (thread) {
delete thread;
}
}

View File

@ -145,8 +145,10 @@ namespace Qv2ray::core::kernel
}
// Write the final configuration to the disk.
QString json = JsonToString(root);
StringToFile(&json, new QFile(QV2RAY_GENERATED_FILE_PATH));
QFile configFile(QV2RAY_GENERATED_FILE_PATH);
StringToFile(&json, &configFile);
//
auto filePath = QV2RAY_GENERATED_FILE_PATH;

View File

@ -329,7 +329,7 @@ int main(int argc, char *argv[])
}
// Load the config for upgrade, but do not parse it to the struct.
auto conf = JsonFromString(StringFromFile(std::make_unique<QFile>(QV2RAY_CONFIG_FILE).get()));
auto conf = JsonFromString(StringFromFile(QV2RAY_CONFIG_FILE));
auto confVersion = conf["config_version"].toVariant().toString().toInt();
if (confVersion > QV2RAY_CONFIG_VERSION) {

View File

@ -8,6 +8,12 @@ class QvInboundNodeModel : public NodeDataModel
Q_OBJECT
public:
explicit QvInboundNodeModel(std::shared_ptr<InboundNodeData> data);
~QvInboundNodeModel()
{
if (_label) {
delete _label;
}
}
QString caption() const override
{

View File

@ -7,6 +7,12 @@ class QvOutboundNodeModel : public NodeDataModel
Q_OBJECT
public:
explicit QvOutboundNodeModel(std::shared_ptr<OutboundNodeData> data);
~QvOutboundNodeModel()
{
if (_label) {
delete _label;
}
}
QString caption() const override
{

View File

@ -8,6 +8,12 @@ class QvRuleNodeDataModel : public NodeDataModel
Q_OBJECT
public:
QvRuleNodeDataModel(std::shared_ptr<RuleNodeData> data);
~QvRuleNodeDataModel()
{
if (_label) {
delete _label;
}
}
QString caption() const override
{

View File

@ -67,11 +67,10 @@ void ImportConfigWindow::on_qrFromScreenBtn_clicked()
QApplication::processEvents();
QThread::msleep(static_cast<ulong>(doubleSpinBox->value() * 1000));
auto w = new ScreenShotWindow();
auto pix = w->DoScreenShot();
auto _r = w->result();
ScreenShotWindow w;
auto pix = w.DoScreenShot();
auto _r = w.result();
// Explicitly delete w to call UNREGISTER_WINDOW
delete w;
if (hideQv2ray) {
messageBus.EmitGlobalSignal(QvMessage::SHOW_WINDOWS);

View File

@ -889,9 +889,9 @@ void MainWindow::on_action_RCM_ConvToComplex_triggered()
bool isChanged = false;
//
LOG(UI, "INFO: Opening route editor.")
RouteEditor *routeWindow = new RouteEditor(outBoundRoot, this);
root = routeWindow->OpenEditor();
isChanged = routeWindow->result() == QDialog::Accepted;
RouteEditor routeWindow(outBoundRoot, this);
root = routeWindow.OpenEditor();
isChanged = routeWindow.result() == QDialog::Accepted;
QString alias = _identifier.connectionName;
if (isChanged) {

View File

@ -22,7 +22,7 @@ PreferencesWindow::PreferencesWindow(QWidget *parent) : QDialog(parent),
{
setupUi(this);
QvMessageBusConnect(PreferencesWindow);
textBrowser->setHtml(StringFromFile(new QFile(":/assets/credit.html")));
textBrowser->setHtml(StringFromFile(":/assets/credit.html"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
//
// Set network Toolbar page state.
@ -820,46 +820,46 @@ void PreferencesWindow::on_pacGoBtn_clicked()
QString fileContent;
pacGoBtn->setEnabled(false);
gfwListCB->setEnabled(false);
auto request = new QvHttpRequestHelper();
QvHttpRequestHelper request;
LOG(PROXY, "Downloading GFWList file.")
bool withProxy = getGFWListWithProxyCB->isChecked();
switch (gfwListCB->currentIndex()) {
case 0:
gfwLocation = "https://gitlab.com/gfwlist/gfwlist/raw/master/gfwlist.txt";
fileContent = QString::fromUtf8(request->syncget(gfwLocation, withProxy));
fileContent = QString::fromUtf8(request.syncget(gfwLocation, withProxy));
break;
case 1:
gfwLocation = "https://pagure.io/gfwlist/raw/master/f/gfwlist.txt";
fileContent = QString::fromUtf8(request->syncget(gfwLocation, withProxy));
fileContent = QString::fromUtf8(request.syncget(gfwLocation, withProxy));
break;
case 2:
gfwLocation = "http://repo.or.cz/gfwlist.git/blob_plain/HEAD:/gfwlist.txt";
fileContent = QString::fromUtf8(request->syncget(gfwLocation, withProxy));
fileContent = QString::fromUtf8(request.syncget(gfwLocation, withProxy));
break;
case 3:
gfwLocation = "https://bitbucket.org/gfwlist/gfwlist/raw/HEAD/gfwlist.txt";
fileContent = QString::fromUtf8(request->syncget(gfwLocation, withProxy));
fileContent = QString::fromUtf8(request.syncget(gfwLocation, withProxy));
break;
case 4:
gfwLocation = "https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt";
fileContent = QString::fromUtf8(request->syncget(gfwLocation, withProxy));
fileContent = QString::fromUtf8(request.syncget(gfwLocation, withProxy));
break;
case 5:
gfwLocation = "https://git.tuxfamily.org/gfwlist/gfwlist.git/plain/gfwlist.txt";
fileContent = QString::fromUtf8(request->syncget(gfwLocation, withProxy));
fileContent = QString::fromUtf8(request.syncget(gfwLocation, withProxy));
break;
case 6:
QFileDialog d;
d.exec();
auto file = d.getOpenFileUrl(this, tr("Select GFWList in base64")).toString();
fileContent = StringFromFile(new QFile(file));
fileContent = StringFromFile(file);
break;
}

File diff suppressed because it is too large Load Diff