From e463ccc22f40c61c5bf3c7cf430d0ecd52f07e77 Mon Sep 17 00:00:00 2001 From: Qv2ray-dev <59914293+Qv2ray-dev@users.noreply.github.com> Date: Sun, 9 Feb 2020 10:38:46 +0800 Subject: [PATCH] fix: fixed several other memory leaks --- Build.Counter | 2 +- src/common/QvHelpers.cpp | 6 + src/common/QvHelpers.hpp | 1 + src/components/pac/QvPACHandler.cpp | 2 +- .../plugins/toolbar/QvToolbar_linux.cpp | 1 + src/core/connection/ConnectionIO.cpp | 17 +- src/core/kernel/APIBackend.cpp | 2 +- src/core/kernel/KernelInteractions.cpp | 4 +- src/main.cpp | 2 +- src/ui/nodemodels/InboundNodeModel.hpp | 6 + src/ui/nodemodels/OutboundNodeModel.hpp | 6 + src/ui/nodemodels/RuleNodeModel.hpp | 6 + src/ui/w_ImportConfig.cpp | 7 +- src/ui/w_MainWindow.cpp | 6 +- src/ui/w_PreferencesWindow.cpp | 18 +- translations/en_US.ts | 438 +++++++++--------- 16 files changed, 271 insertions(+), 253 deletions(-) diff --git a/Build.Counter b/Build.Counter index 4e2ba85f..a7b2ea80 100644 --- a/Build.Counter +++ b/Build.Counter @@ -1 +1 @@ -3801 +3803 diff --git a/src/common/QvHelpers.cpp b/src/common/QvHelpers.cpp index 7d1791ba..367c123e 100644 --- a/src/common/QvHelpers.cpp +++ b/src/common/QvHelpers.cpp @@ -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); diff --git a/src/common/QvHelpers.hpp b/src/common/QvHelpers.hpp index 8e7a0c99..0189adda 100644 --- a/src/common/QvHelpers.hpp +++ b/src/common/QvHelpers.hpp @@ -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); // diff --git a/src/components/pac/QvPACHandler.cpp b/src/components/pac/QvPACHandler.cpp index 74b1727f..d2892049 100644 --- a/src/components/pac/QvPACHandler.cpp +++ b/src/components/pac/QvPACHandler.cpp @@ -38,7 +38,7 @@ namespace Qv2ray::components::pac // DEBUG(PROXY, "PAC Listening local endpoint: " + address + ":" + QSTRN(port)) // - QString gfwContent = StringFromFile(std::make_unique(QV2RAY_RULES_GFWLIST_PATH).get()); + QString gfwContent = StringFromFile(QV2RAY_RULES_GFWLIST_PATH); pacContent = ConvertGFWToPAC(gfwContent, proxyString); // auto result = pacServer->listen(QHostAddress(address), static_cast(port)); diff --git a/src/components/plugins/toolbar/QvToolbar_linux.cpp b/src/components/plugins/toolbar/QvToolbar_linux.cpp index 7833c2c6..78b903aa 100644 --- a/src/components/plugins/toolbar/QvToolbar_linux.cpp +++ b/src/components/plugins/toolbar/QvToolbar_linux.cpp @@ -60,6 +60,7 @@ namespace Qv2ray::components::plugins::Toolbar } server->close(); + delete server; } void StartMessageQThread() { diff --git a/src/core/connection/ConnectionIO.cpp b/src/core/connection/ConnectionIO.cpp index 7a65b16e..e51c421f 100644 --- a/src/core/connection/ConnectionIO.cpp +++ b/src/core/connection/ConnectionIO.cpp @@ -7,7 +7,7 @@ namespace Qv2ray::core::connection { CONFIGROOT _ReadConnection(const QString &connection) { - QString jsonString = StringFromFile(std::make_unique(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) diff --git a/src/core/kernel/APIBackend.cpp b/src/core/kernel/APIBackend.cpp index da60c4a9..6177631e 100644 --- a/src/core/kernel/APIBackend.cpp +++ b/src/core/kernel/APIBackend.cpp @@ -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; } } diff --git a/src/core/kernel/KernelInteractions.cpp b/src/core/kernel/KernelInteractions.cpp index ceeeba5a..e6284630 100644 --- a/src/core/kernel/KernelInteractions.cpp +++ b/src/core/kernel/KernelInteractions.cpp @@ -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; diff --git a/src/main.cpp b/src/main.cpp index f8d2d58b..f04951a1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(QV2RAY_CONFIG_FILE).get())); + auto conf = JsonFromString(StringFromFile(QV2RAY_CONFIG_FILE)); auto confVersion = conf["config_version"].toVariant().toString().toInt(); if (confVersion > QV2RAY_CONFIG_VERSION) { diff --git a/src/ui/nodemodels/InboundNodeModel.hpp b/src/ui/nodemodels/InboundNodeModel.hpp index a492f27c..ba100875 100644 --- a/src/ui/nodemodels/InboundNodeModel.hpp +++ b/src/ui/nodemodels/InboundNodeModel.hpp @@ -8,6 +8,12 @@ class QvInboundNodeModel : public NodeDataModel Q_OBJECT public: explicit QvInboundNodeModel(std::shared_ptr data); + ~QvInboundNodeModel() + { + if (_label) { + delete _label; + } + } QString caption() const override { diff --git a/src/ui/nodemodels/OutboundNodeModel.hpp b/src/ui/nodemodels/OutboundNodeModel.hpp index d23f08f1..52ea5ebf 100644 --- a/src/ui/nodemodels/OutboundNodeModel.hpp +++ b/src/ui/nodemodels/OutboundNodeModel.hpp @@ -7,6 +7,12 @@ class QvOutboundNodeModel : public NodeDataModel Q_OBJECT public: explicit QvOutboundNodeModel(std::shared_ptr data); + ~QvOutboundNodeModel() + { + if (_label) { + delete _label; + } + } QString caption() const override { diff --git a/src/ui/nodemodels/RuleNodeModel.hpp b/src/ui/nodemodels/RuleNodeModel.hpp index ffb589e9..f3765bfe 100644 --- a/src/ui/nodemodels/RuleNodeModel.hpp +++ b/src/ui/nodemodels/RuleNodeModel.hpp @@ -8,6 +8,12 @@ class QvRuleNodeDataModel : public NodeDataModel Q_OBJECT public: QvRuleNodeDataModel(std::shared_ptr data); + ~QvRuleNodeDataModel() + { + if (_label) { + delete _label; + } + } QString caption() const override { diff --git a/src/ui/w_ImportConfig.cpp b/src/ui/w_ImportConfig.cpp index 445f3bc7..3d93f0a5 100644 --- a/src/ui/w_ImportConfig.cpp +++ b/src/ui/w_ImportConfig.cpp @@ -67,11 +67,10 @@ void ImportConfigWindow::on_qrFromScreenBtn_clicked() QApplication::processEvents(); QThread::msleep(static_cast(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); diff --git a/src/ui/w_MainWindow.cpp b/src/ui/w_MainWindow.cpp index 540c7820..feb194df 100644 --- a/src/ui/w_MainWindow.cpp +++ b/src/ui/w_MainWindow.cpp @@ -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) { diff --git a/src/ui/w_PreferencesWindow.cpp b/src/ui/w_PreferencesWindow.cpp index 843fac76..7d86caf2 100644 --- a/src/ui/w_PreferencesWindow.cpp +++ b/src/ui/w_PreferencesWindow.cpp @@ -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; } diff --git a/translations/en_US.ts b/translations/en_US.ts index 60bfb910..d1c4960d 100644 --- a/translations/en_US.ts +++ b/translations/en_US.ts @@ -4,23 +4,23 @@ ConfigExporter - + Save Image - - + + Share Connection - + Image has been copied to the clipboard. - + VMess string has been copied to the clipboard. @@ -241,64 +241,64 @@ - + Select file to import - + Capture QRCode - + Cannot find a valid QRCode from this region. - + Import config file - + Failed to check the validity of the config file. - + Select an image to import - + QRCode scanning failed - + Cannot find any QRCode from the image. - - - + + + Edit file as JSON - + Provided file not found: - + The file you selected has json syntax error. Continue editing may make you lose data. Would you like to continue? - + Failed to save file, please check if you have proper permissions @@ -546,26 +546,26 @@ - - + + Removing a user - - + + You haven't selected a user yet. - - + + Add a user - - + + This user exists already. @@ -599,7 +599,7 @@ - + Json Contains Syntax Errors @@ -609,17 +609,17 @@ - + You must correct these errors before continue. - + Syntax Errors - + Please fix the JSON errors before continue @@ -633,19 +633,19 @@ - + Connect - + Disconnect - + Reconnect @@ -741,7 +741,7 @@ - + Preferences @@ -838,38 +838,38 @@ - - + + Hide - + Quit - + Rename - + Connect to this - + Edit as Complex Config - + Edit as Json - + Share as QRCode/VMess URL @@ -879,8 +879,8 @@ - - + + Show @@ -896,37 +896,37 @@ - - + + No connection selected! - - + + Please select a config from the list. - + Update - + Found a new version: - + Download Link: - - - - + + + + Connected: @@ -978,8 +978,8 @@ - HTTP inbound is not enabled - + Both HTTP and SOCKS inbounds are not enabled + @@ -1007,181 +1007,181 @@ - + Disconnected - + Complex - + Simple - + No data - + ms - + There're no support of sharing configs other than vmess and shadowsocks - - - - - + + + + + N/A - - - + + + Rename a Connection - + V2ray vcore terminated. - + V2ray vcore terminated unexpectedly. - + To solve the problem, read the V2ray log in the log text browser. - + Enable System Proxy - + Disable System Proxy - + System Proxy - + Ping - + Ping All - - + + Subscription - + Already connected to: - + Disconnected from: - + The name cannot be empty - + The name has been used already, Please choose another. - + The name you suggested is not valid, please try another. - + Removing Connection(s) - + Are you sure to remove selected connection(s)? - - + + Removing this Connection - - + + Failed to delete connection file, please delete manually. - + Removing a subscription config - + Do you want to remove the config loaded from a subscription? - - - + + + No Config Selected - - - + + + Please Select a Config - + You are about to run latency test on all servers, do you want to continue? - + Testing... - + Latency Test @@ -1192,7 +1192,7 @@ - + Share Connection @@ -1329,10 +1329,10 @@ PreferencesWindow - - - - + + + + Preferences @@ -1361,16 +1361,6 @@ Language - - - zh-CN - - - - - en-US - - Log Level @@ -1757,13 +1747,13 @@ p, li { white-space: pre-wrap; } - + Bold - + Italic @@ -1873,145 +1863,145 @@ p, li { white-space: pre-wrap; } - + Use Darkmode Theme - + Page - + Item(s) - - + + Enable tProxy Support - + to this path: - + Qv2ray Network Toolbar is disabled and still under test. Add --withToolbarPlugin to enable. - + Duplicated port numbers detected, please check the port number settings. - + Open V2ray assets folder - + Open V2ray core file - + This will append capabilities to the V2ray executable. - + Qv2ray will copy your V2ray core to this path: - + If anything goes wrong after enabling this, please check issue #57 or the link below: - + Qv2ray cannot copy one or both V2ray files from: - - + + Failed to setcap onto V2ray executable. You may need to run `setcap` manually. - + tProxy is not supported on macOS and Windows - + Apply network toolbar settings - + All other modified settings will be applied as well after this object. - + Do you want to continue? - + Dark Mode - + Please restart Qv2ray to fully apply this feature. - + Select GFWList in base64 - + Download GFWList - + Successfully downloaded GFWList. - + Start with boot - + Failed to set auto start option. - - + + V2ray Core Settings - + V2ray path configuration check passed. - + Current version of V2ray is: @@ -2070,114 +2060,114 @@ p, li { white-space: pre-wrap; } - + Warning - + Qv2ray cannot load the config file from here: - + Cannot Start Qv2ray - + Cannot find a place to store config files. - + Qv2ray has searched these paths below: - - - + + + Qv2ray will now exit. - + Failed to initialise Qv2ray - + Failed to determine the location of config file. - + Please report if you think it's a bug. - + You cannot run Qv2ray as root, please use --I-just-wanna-run-with-root if you REALLY want to do so. - + --> USE IT AT YOUR OWN RISK! - + Cannot load languages - + Qv2ray will continue running, but you cannot change the UI language. - + Qv2ray Cannot Continue - + You are running a lower version of Qv2ray compared to the current config file. - + Please check if there's an issue explaining about it. - + Or submit a new issue if you think this is an error. - + Dependency Missing - + This could be caused by a missing of `openssl` package in your system. - + If you are using an AppImage from Github Action, please report a bug. - + Cannot find openssl libs - + Technical Details @@ -2394,7 +2384,7 @@ p, li { white-space: pre-wrap; } QvInboundNodeModel - + Missing or incorrect inputs @@ -2402,7 +2392,7 @@ p, li { white-space: pre-wrap; } QvOutboundNodeModel - + Missing or incorrect inputs @@ -2410,7 +2400,7 @@ p, li { white-space: pre-wrap; } QvRuleNodeDataModel - + Missing or incorrect inputs @@ -2419,7 +2409,7 @@ p, li { white-space: pre-wrap; } RouteEditor - + Route Editor @@ -2674,47 +2664,47 @@ p, li { white-space: pre-wrap; } - - - + + + OK - + Cannot Edit - + This outbound entry is not supported by the GUI editor. - - + + We will launch Json Editor instead. - + Show rule details - + A rule cannot be found: - - - + + + Protocol list changed: - + Balancer is empty, not processing. @@ -2746,81 +2736,81 @@ p, li { white-space: pre-wrap; } - + To make this rule ready to use, you need to connect it to an outbound node. - + Remove Items - - + + Please select a node from the graph to continue. - + Error - + Qv2ray entered an unknown state. - + Edit Inbound/Outbound - - + + Edit Inbound - - + + No inbound tag found: - - + + Opening JSON editor - + Unsupported Outbound Type - + Opening default outbound editor. - + Added DIRECT outbound - + Currently, this type of outbound is not supported by the editor. - + Opening default inbound editor - + Removed a balancer entry. @@ -3044,50 +3034,50 @@ p, li { white-space: pre-wrap; } SubscribeEditor - - - - + + + + Renaming a subscription - + The subscription name is invalid, please try another. - + New name of this subscription has been used already, please suggest another one - + Failed to rename a subscription, this is an unknown error. - + Successfully renamed a subscription - + Update Subscription - + Would you like to reload this subscription from the Url? - + Updating subscriptions - + Failed to process the result from the upstream, please check your Url.