fixed memleak and uninitilized value

This commit is contained in:
mrbesen 2022-07-05 23:56:09 +02:00
parent e2f04637f2
commit e893852bc1
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
4 changed files with 22 additions and 3 deletions

16
include/defer.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
// from: https://www.gingerbill.org/article/2015/08/19/defer-in-cpp/
#include <functional>
struct privDefer {
std::function<void()> f;
privDefer(std::function<void()> f) : f(f) {}
~privDefer() { f(); }
};
#define DEFER_1(x, y) x##y
#define DEFER_2(x, y) DEFER_1(x, y)
#define DEFER_3(x) DEFER_2(x, __COUNTER__)
#define defer(code) auto DEFER_3(_defer_) = privDefer([&](){code;})

View File

@ -47,7 +47,7 @@ private:
Config::PositionConfig* conf;
DataDragon* dd = nullptr;
Position position;
Position position = Position::INVALID;
};
#endif // SETTINGSTAB_H

View File

@ -60,6 +60,7 @@ HEADERS += \
include/config.h \
include/datadragon.h \
include/datadragonimagecache.h \
include/defer.h \
include/files.h \
include/json.h \
include/lolautoaccept.h \

View File

@ -16,6 +16,8 @@
#include <Log.h>
#include "defer.h"
static bool endsWith(const std::string& all, const std::string& end) {
return all.rfind(end) == all.size() - end.size();
}
@ -42,6 +44,8 @@ std::shared_ptr<ClientAccess> ClientAccess::find(bool uselockfile) {
DIR* procdir = opendir("/proc");
if(!procdir) return nullptr;
defer( closedir(procdir) );
dirent* entry = nullptr;
while ((entry = readdir(procdir)) != NULL) {
if (entry->d_type != DT_DIR) continue;
@ -82,8 +86,6 @@ std::shared_ptr<ClientAccess> ClientAccess::find(bool uselockfile) {
}
}
closedir(procdir);
return nullptr;
}