Skip to content

Commit c031e4c

Browse files
committed
gc: fix instance allocation counting
A class destructor is not called if an exception is thrown when executing the constructor. This caused an issue where if an error occured during construction of NSFW objects, the instance coutn would never be decremented. This commit moves the increment step at the end of the constructor, after which nothing can error out. Signed-off-by: Paul Maréchal <[email protected]>
1 parent 94e83f0 commit c031e4c

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

js/spec/index-spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,15 @@ describe('Node Sentinel File Watcher', function() {
684684
describe('Garbage collection', function() {
685685
it('can garbage collect all instances', async function () {
686686
this.timeout(60000);
687+
let threw = false;
688+
try {
689+
// Try to get an error coming from the C++ constructor by passing a bad callback
690+
await nsfw(workDir, () => {}, { errorCallback: 'not a callback' });
691+
} catch (e) {
692+
threw = true;
693+
} if (!threw) {
694+
assert.fail('nsfw must throw when provided a bad callback');
695+
}
687696
while (nsfw.getAllocatedInstanceCount() > 0) {
688697
global.gc();
689698
await sleep(0);

src/NSFW.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ NSFW::NSFW(const Napi::CallbackInfo &info):
1212
mPath(""),
1313
mRunning(false)
1414
{
15-
if (gcEnabled) {
16-
instanceCount++;
17-
}
18-
1915
auto env = info.Env();
2016
if (info.Length() < 1 || !info[0].IsString()) {
2117
throw Napi::TypeError::New(env, "Must pass a string path as the first argument to NSFW.");
@@ -71,6 +67,10 @@ NSFW::NSFW(const Napi::CallbackInfo &info):
7167
1
7268
);
7369
}
70+
71+
if (gcEnabled) {
72+
instanceCount++;
73+
}
7474
}
7575

7676
NSFW::~NSFW() {

0 commit comments

Comments
 (0)