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>
This commit is contained in:
Berthold Stoeger 2020-03-09 17:56:32 +01:00 committed by Dirk Hohndel
parent b15b9c6cd0
commit 2fcd4fff0a
2 changed files with 17 additions and 15 deletions

View file

@ -381,13 +381,17 @@ void QMLManager::updateAllGlobalLists()
updateSiteList(); updateSiteList();
} }
static QString nocloud_localstorage()
{
return QString(system_default_directory()) + "/cloudstorage/localrepo[master]";
}
void QMLManager::mergeLocalRepo() void QMLManager::mergeLocalRepo()
{ {
char *filename = NOCLOUD_LOCALSTORAGE;
struct dive_table table = empty_dive_table; struct dive_table table = empty_dive_table;
struct trip_table trips = empty_trip_table; struct trip_table trips = empty_trip_table;
struct dive_site_table sites = empty_dive_site_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); add_imported_dives(&table, &trips, &sites, IMPORT_MERGE_ALL_TRIPS);
} }
@ -460,7 +464,7 @@ void QMLManager::finishSetup()
} else if (!empty_string(existing_filename) && } else if (!empty_string(existing_filename) &&
qPrefCloudStorage::cloud_verification_status() != qPrefCloudStorage::CS_UNKNOWN) { qPrefCloudStorage::cloud_verification_status() != qPrefCloudStorage::CS_UNKNOWN) {
setOldStatus((qPrefCloudStorage::cloud_status)qPrefCloudStorage::cloud_verification_status()); 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); qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_NOCLOUD);
saveCloudCredentials(qPrefCloudStorage::cloud_storage_email(), qPrefCloudStorage::cloud_storage_password(), qPrefCloudStorage::cloud_storage_pin()); saveCloudCredentials(qPrefCloudStorage::cloud_storage_email(), qPrefCloudStorage::cloud_storage_password(), qPrefCloudStorage::cloud_storage_pin());
appendTextToLog(tr("working in no-cloud mode")); appendTextToLog(tr("working in no-cloud mode"));
@ -820,7 +824,7 @@ void QMLManager::revertToNoCloudIfNeeded()
qPrefCloudStorage::set_cloud_storage_password(""); qPrefCloudStorage::set_cloud_storage_password("");
setOldStatus((qPrefCloudStorage::cloud_status)qPrefCloudStorage::cloud_verification_status()); setOldStatus((qPrefCloudStorage::cloud_status)qPrefCloudStorage::cloud_verification_status());
qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_NOCLOUD); 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); setStartPageText(RED_FONT + tr("Failed to connect to cloud server, reverting to no cloud status") + END_FONT);
} }
alreadySaving = false; alreadySaving = false;
@ -1292,17 +1296,17 @@ void QMLManager::openNoCloudRepo()
* is obviously empty when just created. * is obviously empty when just created.
*/ */
{ {
char *filename = NOCLOUD_LOCALSTORAGE; QString filename = nocloud_localstorage();
const char *branch; const char *branch;
struct git_repository *git; 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) { if (git == dummy_git_repository) {
git_create_local_repo(filename); git_create_local_repo(qPrintable(filename));
set_filename(filename); set_filename(qPrintable(filename));
auto s = qPrefLog::instance(); auto s = qPrefLog::instance();
s->set_default_filename(filename); s->set_default_filename(qPrintable(filename));
s->set_default_file_behavior(LOCAL_DEFAULT_FILE); s->set_default_file_behavior(LOCAL_DEFAULT_FILE);
} }
@ -1314,11 +1318,11 @@ void QMLManager::saveChangesLocal()
if (unsaved_changes()) { if (unsaved_changes()) {
if (qPrefCloudStorage::cloud_verification_status() == qPrefCloudStorage::CS_NOCLOUD) { if (qPrefCloudStorage::cloud_verification_status() == qPrefCloudStorage::CS_NOCLOUD) {
if (empty_string(existing_filename)) { if (empty_string(existing_filename)) {
char *filename = NOCLOUD_LOCALSTORAGE; QString filename = nocloud_localstorage();
git_create_local_repo(filename); git_create_local_repo(qPrintable(filename));
set_filename(filename); set_filename(qPrintable(filename));
auto s = qPrefLog::instance(); auto s = qPrefLog::instance();
s->set_default_filename(filename); s->set_default_filename(qPrintable(filename));
s->set_default_file_behavior(LOCAL_DEFAULT_FILE); s->set_default_file_behavior(LOCAL_DEFAULT_FILE);
} }
} else if (!m_loadFromCloud) { } else if (!m_loadFromCloud) {

View file

@ -19,8 +19,6 @@
#include "core/settings/qPrefCloudStorage.h" #include "core/settings/qPrefCloudStorage.h"
#include "core/subsurface-qt/divelistnotifier.h" #include "core/subsurface-qt/divelistnotifier.h"
#define NOCLOUD_LOCALSTORAGE format_string("%s/cloudstorage/localrepo[master]", system_default_directory())
class QMLManager : public QObject { class QMLManager : public QObject {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString logText READ logText WRITE setLogText NOTIFY logTextChanged) Q_PROPERTY(QString logText READ logText WRITE setLogText NOTIFY logTextChanged)