Skip to content

Commit 978fa0a

Browse files
committed
refactor: avoid memory leak in IniHelper
1 parent 4053a41 commit 978fa0a

4 files changed

Lines changed: 34 additions & 36 deletions

File tree

src/legacy/api/DataAPI.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,13 @@ bool ConfJsonClass::reload() {
269269
ConfIniClass::ConfIniClass(const Local<Object>& scriptObj, const string& path, const string& defContent)
270270
: ScriptClass(scriptObj),
271271
ConfBaseClass(path) {
272-
iniConf = SimpleIni::create(path, defContent);
272+
iniConf = SimpleIni::create(path, defContent).release();
273273
}
274274

275275
ConfIniClass::ConfIniClass(const string& path, const string& defContent)
276276
: ScriptClass(ScriptClass::ConstructFromCpp<ConfIniClass>{}),
277277
ConfBaseClass(path) {
278-
iniConf = SimpleIni::create(path, defContent);
278+
iniConf = SimpleIni::create(path, defContent).release();
279279
}
280280

281281
ConfIniClass::~ConfIniClass() { close(); }
@@ -309,7 +309,7 @@ bool ConfIniClass::reload() {
309309
if (!isValid()) return false;
310310

311311
delete iniConf;
312-
iniConf = SimpleIni::create(confPath, "");
312+
iniConf = SimpleIni::create(confPath).release();
313313
return true;
314314
}
315315

src/legacy/api/DataAPI.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class DataClass {
3333
};
3434
extern ClassDefine<void> DataClassBuilder;
3535

36-
Local<Array> objectificationMoneyHistory(const string& res);
36+
Local<Array> objectificationMoneyHistory(const std::string& res);
3737

3838
//////////////////// Money Static ////////////////////
3939

@@ -59,7 +59,7 @@ class ConfBaseClass {
5959
virtual bool reload() = 0;
6060

6161
public:
62-
explicit ConfBaseClass(const string& dir);
62+
explicit ConfBaseClass(const std::string& dir);
6363

6464
virtual Local<Value> reload(const Arguments& args) = 0;
6565
virtual Local<Value> close(const Arguments& args) = 0;
@@ -77,8 +77,8 @@ class ConfJsonClass : public ScriptClass, public ConfBaseClass {
7777
bool reload() override;
7878

7979
public:
80-
explicit ConfJsonClass(const Local<Object>& scriptObj, const string& path, const string& defContent);
81-
explicit ConfJsonClass(const string& path, const string& defContent);
80+
explicit ConfJsonClass(const Local<Object>& scriptObj, const std::string& path, const std::string& defContent);
81+
explicit ConfJsonClass(const std::string& path, const std::string& defContent);
8282
~ConfJsonClass();
8383
static ConfJsonClass* constructor(const Arguments& args);
8484

@@ -91,7 +91,7 @@ class ConfJsonClass : public ScriptClass, public ConfBaseClass {
9191
virtual Local<Value> write(const Arguments& args) override;
9292

9393
// For Compatibility
94-
static Local<Value> newConf(const string& path, const string& defContent = "");
94+
static Local<Value> newConf(const std::string& path, const std::string& defContent = "");
9595
};
9696
extern ClassDefine<ConfJsonClass> ConfJsonClassBuilder;
9797

@@ -103,8 +103,8 @@ class ConfIniClass : public ScriptClass, public ConfBaseClass {
103103
bool reload() override;
104104

105105
public:
106-
explicit ConfIniClass(const Local<Object>& scriptObj, const string& path, const string& defContent);
107-
explicit ConfIniClass(const string& path, const string& defContent);
106+
explicit ConfIniClass(const Local<Object>& scriptObj, const std::string& path, const std::string& defContent);
107+
explicit ConfIniClass(const std::string& path, const std::string& defContent);
108108
~ConfIniClass();
109109
static ConfIniClass* constructor(const Arguments& args);
110110

@@ -122,6 +122,6 @@ class ConfIniClass : public ScriptClass, public ConfBaseClass {
122122
virtual Local<Value> write(const Arguments& args) override;
123123

124124
// For Compatibility
125-
static Local<Value> newConf(const string& path, const string& defContent = "");
125+
static Local<Value> newConf(const std::string& path, const std::string& defContent = "");
126126
};
127127
extern ClassDefine<ConfIniClass> ConfIniClassBuilder;

src/legacy/utils/IniHelper.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <filesystem>
77

8-
SimpleIni* SimpleIni::create(const std::string& path, const std::string& defContent) {
8+
std::unique_ptr<SimpleIni> SimpleIni::create(const std::string& path, const std::string& defContent) {
99
if (!std::filesystem::exists(ll::string_utils::str2wstr(path))) {
1010
// 创建新的
1111
std::filesystem::create_directories(
@@ -18,59 +18,58 @@ SimpleIni* SimpleIni::create(const std::string& path, const std::string& defCont
1818
}
1919

2020
// 已存在
21-
auto root = new SimpleIni;
21+
auto root = std::make_unique<SimpleIni>();
2222
root->SetUnicode(true);
2323
auto res = root->LoadFile(path.c_str());
2424
if (res < 0) {
2525
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().error("Failed in loading ini file");
2626
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().error(
27-
string("Error Code:") + std::to_string((int)res)
27+
std::string("Error Code:") + std::to_string((int)res)
2828
);
29-
delete root;
3029
return nullptr;
3130
} else {
3231
root->filePath = path;
3332
return root;
3433
}
3534
}
3635

37-
bool SimpleIni::setInt(const string& sec, const string& key, int value) {
36+
bool SimpleIni::setInt(const std::string& sec, const std::string& key, int value) {
3837
bool isOk = SetLongValue(sec.c_str(), key.c_str(), value) >= 0;
3938
SaveFile(filePath.c_str());
4039
return isOk;
4140
}
4241

43-
bool SimpleIni::setFloat(const string& sec, const string& key, float value) {
42+
bool SimpleIni::setFloat(const std::string& sec, const std::string& key, float value) {
4443
bool isOk = SetDoubleValue(sec.c_str(), key.c_str(), value) >= 0;
4544
SaveFile(filePath.c_str());
4645
return isOk;
4746
}
4847

49-
bool SimpleIni::setString(const string& sec, const string& key, const string& value) {
48+
bool SimpleIni::setString(const std::string& sec, const std::string& key, const std::string& value) {
5049
bool isOk = SetValue(sec.c_str(), key.c_str(), value.c_str()) >= 0;
5150
SaveFile(filePath.c_str());
5251
return isOk;
5352
}
5453

55-
bool SimpleIni::setBool(const string& sec, const string& key, bool value) {
54+
bool SimpleIni::setBool(const std::string& sec, const std::string& key, bool value) {
5655
bool isOk = SetBoolValue(sec.c_str(), key.c_str(), value) >= 0;
5756
SaveFile(filePath.c_str());
5857
return isOk;
5958
}
6059

61-
int SimpleIni::getInt(const string& sec, const string& key, int def) {
60+
int SimpleIni::getInt(const std::string& sec, const std::string& key, int def) {
6261
return GetLongValue(sec.c_str(), key.c_str(), def);
6362
}
6463

65-
float SimpleIni::getFloat(const string& sec, const string& key, float def) {
64+
float SimpleIni::getFloat(const std::string& sec, const std::string& key, float def) {
6665
return (float)GetDoubleValue(sec.c_str(), key.c_str(), def);
6766
}
6867

69-
string SimpleIni::getString(const string& sec, const string& key, const string& def) {
68+
std::string SimpleIni::getString(const std::string& sec, const std::string& key, const std::string& def) {
7069
return GetValue(sec.c_str(), key.c_str(), def.c_str());
7170
}
7271

73-
bool SimpleIni::getBool(const string& sec, const string& key, bool def) {
72+
bool SimpleIni::getBool(const std::string& sec, const std::string& key, bool def) {
7473
return GetBoolValue(sec.c_str(), key.c_str(), def);
7574
}
7675

src/legacy/utils/IniHelper.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
#pragma once
22
#include "SimpleIni.h"
33

4+
#include <memory>
45
#include <string>
56

6-
using std::string;
7-
87
class SimpleIni : public CSimpleIniA {
98
public:
109
std::string filePath;
1110

12-
static inline SimpleIni* create(const std::string& path) { return create(path, ""); }
13-
static SimpleIni* create(const std::string& path, const std::string& defContent);
11+
static inline std::unique_ptr<SimpleIni> create(const std::string& path) { return create(path, ""); }
12+
static std::unique_ptr<SimpleIni> create(const std::string& path, const std::string& defContent);
1413

15-
bool setInt(const string& sec, const string& key, int value);
16-
bool setFloat(const string& sec, const string& key, float value);
17-
bool setString(const string& sec, const string& key, const string& value);
18-
bool setBool(const string& sec, const string& key, bool value);
19-
int getInt(const string& sec, const string& key, int def);
20-
float getFloat(const string& sec, const string& key, float def);
21-
string getString(const string& sec, const string& key, const string& def);
22-
bool getBool(const string& sec, const string& key, bool def);
23-
bool deleteKey(const std::string& sec, const std::string& key);
14+
bool setInt(const std::string& sec, const std::string& key, int value);
15+
bool setFloat(const std::string& sec, const std::string& key, float value);
16+
bool setString(const std::string& sec, const std::string& key, const std::string& value);
17+
bool setBool(const std::string& sec, const std::string& key, bool value);
18+
int getInt(const std::string& sec, const std::string& key, int def);
19+
float getFloat(const std::string& sec, const std::string& key, float def);
20+
std::string getString(const std::string& sec, const std::string& key, const std::string& def);
21+
bool getBool(const std::string& sec, const std::string& key, bool def);
22+
bool deleteKey(const std::string& sec, const std::string& key);
2423
};

0 commit comments

Comments
 (0)