Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak on Linux/Darwin platforms #1572

Merged
merged 1 commit into from
Jun 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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