diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-03-09 17:56:32 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-03-09 12:11:01 -0700 |
commit | 2fcd4fff0a44340c1d22857373d2e88cf6641160 (patch) | |
tree | 5b4c4afa0c15a7545356ca35e0fa635e5899f854 | |
parent | b15b9c6cd0e8d83ec12a868c10cf184217f51309 (diff) | |
download | subsurface-2fcd4fff0a44340c1d22857373d2e88cf6641160.tar.gz |
cleanup: fix NOCLOUD_LOCALSTORAGE leak
The macro NOCLOUD_LOCALSTORAGE creates the path to the local git
repository as a C-string. None of the users were freeing the string
and thus leaking memory.
Replace the macro by an inline function that creates a QString
and pass down to C-functions using the qPrintable() macro.
Note that every qPrintable() invocation does a UTF16->UTF8
conversion. This could be avoided by either using a std::string
or a QByteArray. However, we are talking about microseconds of
CPU time in operations that typically take seconds. Not worth
it.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | mobile-widgets/qmlmanager.cpp | 30 | ||||
-rw-r--r-- | mobile-widgets/qmlmanager.h | 2 |
2 files changed, 17 insertions, 15 deletions
diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp index 17ceebd3b..dffdc9cc5 100644 --- a/mobile-widgets/qmlmanager.cpp +++ b/mobile-widgets/qmlmanager.cpp @@ -381,13 +381,17 @@ void QMLManager::updateAllGlobalLists() updateSiteList(); } +static QString nocloud_localstorage() +{ + return QString(system_default_directory()) + "/cloudstorage/localrepo[master]"; +} + void QMLManager::mergeLocalRepo() { - char *filename = NOCLOUD_LOCALSTORAGE; struct dive_table table = empty_dive_table; struct trip_table trips = empty_trip_table; struct dive_site_table sites = empty_dive_site_table; - parse_file(filename, &table, &trips, &sites); + parse_file(qPrintable(nocloud_localstorage()), &table, &trips, &sites); add_imported_dives(&table, &trips, &sites, IMPORT_MERGE_ALL_TRIPS); } @@ -460,7 +464,7 @@ void QMLManager::finishSetup() } else if (!empty_string(existing_filename) && qPrefCloudStorage::cloud_verification_status() != qPrefCloudStorage::CS_UNKNOWN) { setOldStatus((qPrefCloudStorage::cloud_status)qPrefCloudStorage::cloud_verification_status()); - set_filename(NOCLOUD_LOCALSTORAGE); + set_filename(qPrintable(nocloud_localstorage())); qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_NOCLOUD); saveCloudCredentials(qPrefCloudStorage::cloud_storage_email(), qPrefCloudStorage::cloud_storage_password(), qPrefCloudStorage::cloud_storage_pin()); appendTextToLog(tr("working in no-cloud mode")); @@ -820,7 +824,7 @@ void QMLManager::revertToNoCloudIfNeeded() qPrefCloudStorage::set_cloud_storage_password(""); setOldStatus((qPrefCloudStorage::cloud_status)qPrefCloudStorage::cloud_verification_status()); qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_NOCLOUD); - set_filename(NOCLOUD_LOCALSTORAGE); + set_filename(qPrintable(nocloud_localstorage())); setStartPageText(RED_FONT + tr("Failed to connect to cloud server, reverting to no cloud status") + END_FONT); } alreadySaving = false; @@ -1292,17 +1296,17 @@ void QMLManager::openNoCloudRepo() * is obviously empty when just created. */ { - char *filename = NOCLOUD_LOCALSTORAGE; + QString filename = nocloud_localstorage(); const char *branch; struct git_repository *git; - git = is_git_repository(filename, &branch, NULL, false); + git = is_git_repository(qPrintable(filename), &branch, NULL, false); if (git == dummy_git_repository) { - git_create_local_repo(filename); - set_filename(filename); + git_create_local_repo(qPrintable(filename)); + set_filename(qPrintable(filename)); auto s = qPrefLog::instance(); - s->set_default_filename(filename); + s->set_default_filename(qPrintable(filename)); s->set_default_file_behavior(LOCAL_DEFAULT_FILE); } @@ -1314,11 +1318,11 @@ void QMLManager::saveChangesLocal() if (unsaved_changes()) { if (qPrefCloudStorage::cloud_verification_status() == qPrefCloudStorage::CS_NOCLOUD) { if (empty_string(existing_filename)) { - char *filename = NOCLOUD_LOCALSTORAGE; - git_create_local_repo(filename); - set_filename(filename); + QString filename = nocloud_localstorage(); + git_create_local_repo(qPrintable(filename)); + set_filename(qPrintable(filename)); auto s = qPrefLog::instance(); - s->set_default_filename(filename); + s->set_default_filename(qPrintable(filename)); s->set_default_file_behavior(LOCAL_DEFAULT_FILE); } } else if (!m_loadFromCloud) { diff --git a/mobile-widgets/qmlmanager.h b/mobile-widgets/qmlmanager.h index 145a3df17..74addbc50 100644 --- a/mobile-widgets/qmlmanager.h +++ b/mobile-widgets/qmlmanager.h @@ -19,8 +19,6 @@ #include "core/settings/qPrefCloudStorage.h" #include "core/subsurface-qt/divelistnotifier.h" -#define NOCLOUD_LOCALSTORAGE format_string("%s/cloudstorage/localrepo[master]", system_default_directory()) - class QMLManager : public QObject { Q_OBJECT Q_PROPERTY(QString logText READ logText WRITE setLogText NOTIFY logTextChanged) |