Skip to content

Commit 1a18c33

Browse files
Merge pull request #7048 from nimrod-becker/backport_to_5_11
[Backport to 5.11] fix small mem leaks that accumulate
2 parents 92069ea + 3e0a9de commit 1a18c33

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/native/chunk/splitter_napi.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace noobaa
1111
static Napi::Value _chunk_splitter(const Napi::CallbackInfo& info);
1212
static Napi::Value _splitter_finish(Napi::Env env, Splitter* splitter);
1313
static Napi::Value _splitter_result(Napi::Env env, Splitter* splitter);
14+
static void _free_splitter(Napi::Env env, Splitter* splitter);
1415

1516
void
1617
splitter_napi(Napi::Env env, Napi::Object exports)
@@ -111,7 +112,7 @@ _chunk_splitter(const Napi::CallbackInfo& info)
111112
throw Napi::Error::New(info.Env(), "Invalid splitter config");
112113
}
113114
splitter = new Splitter(min_chunk, max_chunk, avg_chunk_bits, calc_md5, calc_sha256);
114-
state["splitter"] = Napi::External<Splitter>::New(info.Env(), splitter);
115+
state["splitter"] = Napi::External<Splitter>::New(info.Env(), splitter, _free_splitter);
115116
}
116117

117118
auto buffers = info[1].As<Napi::Array>();
@@ -169,4 +170,10 @@ _splitter_result(Napi::Env env, Splitter* splitter)
169170
return arr;
170171
}
171172

173+
static void
174+
_free_splitter(Napi::Env env, Splitter* splitter)
175+
{
176+
delete splitter;
177+
}
178+
172179
} // namespace noobaa

src/native/fs/fs_napi.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,21 +1156,30 @@ struct RealPath : public FSWorker
11561156

11571157
RealPath(const Napi::CallbackInfo& info)
11581158
: FSWorker(info)
1159+
, _full_path(0)
11591160
{
11601161
_path = info[1].As<Napi::String>();
11611162
Begin(XSTR() << DVAL(_path));
11621163
}
11631164

1165+
~RealPath() {
1166+
if (_full_path) {
1167+
free(_full_path);
1168+
_full_path = 0;
1169+
}
1170+
}
1171+
11641172
virtual void Work()
11651173
{
1174+
// realpath called with resolved_path = NULL so it will malloc as needed for the result
1175+
// and we just need to make sure to capture this result and remember to free it on dtor.
11661176
_full_path = realpath(_path.c_str(), NULL);
1167-
if (_full_path == NULL) SetSyscallError();
1177+
if (!_full_path) SetSyscallError();
11681178
}
11691179

11701180
virtual void OnOK()
11711181
{
11721182
DBG1("FS::RealPath::OnOK: " << DVAL(_path) << DVAL(_full_path));
1173-
11741183
Napi::Env env = Env();
11751184
auto res = Napi::String::New(env, _full_path);
11761185
_deferred.Resolve(res);

0 commit comments

Comments
 (0)