Skip to content

Commit 9006bd6

Browse files
shodan8192reconbot
authored andcommitted
fix: memory leak in unix serialport poller (#1572)
1 parent d829ada commit 9006bd6

File tree

5 files changed

+20
-0
lines changed

5 files changed

+20
-0
lines changed

lib/bindings/darwin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class DarwinBinding extends BaseBinding {
4747
.then(() => {
4848
const fd = this.fd;
4949
this.poller.stop();
50+
this.poller.destroy();
5051
this.poller = null;
5152
this.openOptions = null;
5253
this.fd = null;

lib/bindings/linux.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class LinuxBinding extends BaseBinding {
4747
.then(() => {
4848
const fd = this.fd;
4949
this.poller.stop();
50+
this.poller.destroy();
5051
this.poller = null;
5152
this.openOptions = null;
5253
this.fd = null;

lib/bindings/poller.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ class Poller extends EventEmitter {
8989
stop() {
9090
logger('Stopping poller');
9191
this.poller.stop();
92+
this.emitCanceled();
93+
}
94+
95+
destroy() {
96+
logger('Destroying poller');
97+
this.poller.destroy();
98+
this.emitCanceled();
99+
}
100+
101+
emitCanceled() {
92102
const err = new Error('Canceled');
93103
err.canceled = true;
94104
this.emit('readable', err);

src/poller.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ NAN_MODULE_INIT(Poller::Init) {
7676

7777
Nan::SetPrototypeMethod(tpl, "poll", poll);
7878
Nan::SetPrototypeMethod(tpl, "stop", stop);
79+
Nan::SetPrototypeMethod(tpl, "destroy", destroy);
7980

8081
constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
8182
Nan::Set(target, Nan::New("Poller").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
@@ -122,6 +123,12 @@ NAN_METHOD(Poller::stop) {
122123
obj->stop();
123124
}
124125

126+
NAN_METHOD(Poller::destroy) {
127+
Poller* obj = Nan::ObjectWrap::Unwrap<Poller>(info.Holder());
128+
obj->persistent().Reset();
129+
delete obj;
130+
}
131+
125132
inline Nan::Persistent<v8::Function> & Poller::constructor() {
126133
static Nan::Persistent<v8::Function> my_constructor;
127134
return my_constructor;

src/poller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Poller : public Nan::ObjectWrap {
2626
static NAN_METHOD(New);
2727
static NAN_METHOD(poll);
2828
static NAN_METHOD(stop);
29+
static NAN_METHOD(destroy);
2930
static inline Nan::Persistent<v8::Function> & constructor();
3031
};
3132

0 commit comments

Comments
 (0)