From 9006bd61f4ee3f3c654f27c7aafdc171d34e5377 Mon Sep 17 00:00:00 2001 From: shodan8192 Date: Sun, 24 Jun 2018 03:37:42 +0200 Subject: [PATCH] fix: memory leak in unix serialport poller (#1572) --- lib/bindings/darwin.js | 1 + lib/bindings/linux.js | 1 + lib/bindings/poller.js | 10 ++++++++++ src/poller.cpp | 7 +++++++ src/poller.h | 1 + 5 files changed, 20 insertions(+) diff --git a/lib/bindings/darwin.js b/lib/bindings/darwin.js index 08f07ac89..43ac2ac6b 100644 --- a/lib/bindings/darwin.js +++ b/lib/bindings/darwin.js @@ -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; diff --git a/lib/bindings/linux.js b/lib/bindings/linux.js index 17d99318b..7ac32f264 100644 --- a/lib/bindings/linux.js +++ b/lib/bindings/linux.js @@ -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; diff --git a/lib/bindings/poller.js b/lib/bindings/poller.js index 9bba0c2df..476ad7ec9 100644 --- a/lib/bindings/poller.js +++ b/lib/bindings/poller.js @@ -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); diff --git a/src/poller.cpp b/src/poller.cpp index 38c79c2c9..68f83add8 100644 --- a/src/poller.cpp +++ b/src/poller.cpp @@ -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()); @@ -122,6 +123,12 @@ NAN_METHOD(Poller::stop) { obj->stop(); } +NAN_METHOD(Poller::destroy) { + Poller* obj = Nan::ObjectWrap::Unwrap(info.Holder()); + obj->persistent().Reset(); + delete obj; +} + inline Nan::Persistent & Poller::constructor() { static Nan::Persistent my_constructor; return my_constructor; diff --git a/src/poller.h b/src/poller.h index 622b44dab..7b775cc26 100644 --- a/src/poller.h +++ b/src/poller.h @@ -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 & constructor(); };