Skip to content

Commit

Permalink
fix: memory leak in unix serialport poller (#1572)
Browse files Browse the repository at this point in the history
  • Loading branch information
shodan8192 authored and reconbot committed Jun 24, 2018
1 parent d829ada commit 9006bd6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/bindings/darwin.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class DarwinBinding extends BaseBinding {
.then(() => {
const fd = this.fd;
this.poller.stop();
this.poller.destroy();
this.poller = null;
this.openOptions = null;
this.fd = null;
Expand Down
1 change: 1 addition & 0 deletions lib/bindings/linux.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class LinuxBinding extends BaseBinding {
.then(() => {
const fd = this.fd;
this.poller.stop();
this.poller.destroy();
this.poller = null;
this.openOptions = null;
this.fd = null;
Expand Down
10 changes: 10 additions & 0 deletions lib/bindings/poller.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ class Poller extends EventEmitter {
stop() {
logger('Stopping poller');
this.poller.stop();
this.emitCanceled();
}

destroy() {
logger('Destroying poller');
this.poller.destroy();
this.emitCanceled();
}

emitCanceled() {
const err = new Error('Canceled');
err.canceled = true;
this.emit('readable', err);
Expand Down
7 changes: 7 additions & 0 deletions src/poller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ NAN_MODULE_INIT(Poller::Init) {

Nan::SetPrototypeMethod(tpl, "poll", poll);
Nan::SetPrototypeMethod(tpl, "stop", stop);
Nan::SetPrototypeMethod(tpl, "destroy", destroy);

constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
Nan::Set(target, Nan::New("Poller").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
Expand Down Expand Up @@ -122,6 +123,12 @@ NAN_METHOD(Poller::stop) {
obj->stop();
}

NAN_METHOD(Poller::destroy) {
Poller* obj = Nan::ObjectWrap::Unwrap<Poller>(info.Holder());
obj->persistent().Reset();
delete obj;
}

inline Nan::Persistent<v8::Function> & Poller::constructor() {
static Nan::Persistent<v8::Function> my_constructor;
return my_constructor;
Expand Down
1 change: 1 addition & 0 deletions src/poller.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Poller : public Nan::ObjectWrap {
static NAN_METHOD(New);
static NAN_METHOD(poll);
static NAN_METHOD(stop);
static NAN_METHOD(destroy);
static inline Nan::Persistent<v8::Function> & constructor();
};

Expand Down

0 comments on commit 9006bd6

Please sign in to comment.