diff options
author | Anna Henningsen <anna@addaleax.net> | 2017-09-09 23:29:08 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-05-10 15:15:17 +0300 |
commit | 61fd027096c0416a6e9bbe3ee7b7edb4c180725a (patch) | |
tree | e64998f427bfaa959448f7e28223b9d10f01e5eb /src/base_object-inl.h | |
parent | 1db0039c505c6081f1d0d84cc24133ed6659e539 (diff) |
src: use cleanup hooks to tear down BaseObjects
Clean up after `BaseObject` instances when the `Environment`
is being shut down. This takes care of closing non-libuv resources
like `zlib` instances, which do not require asynchronous shutdown.
Many thanks for Stephen Belanger, Timothy Gu and Alexey Orlenko for
reviewing the original version of this commit in the Ayo.js project.
Refs: https://github.com/ayojs/ayo/pull/88
PR-URL: https://github.com/nodejs/node/pull/19377
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/base_object-inl.h')
-rw-r--r-- | src/base_object-inl.h | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/src/base_object-inl.h b/src/base_object-inl.h index 786e1f26b48..3bd854639b2 100644 --- a/src/base_object-inl.h +++ b/src/base_object-inl.h @@ -37,10 +37,13 @@ BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object) CHECK_EQ(false, object.IsEmpty()); CHECK_GT(object->InternalFieldCount(), 0); object->SetAlignedPointerInInternalField(0, static_cast<void*>(this)); + env_->AddCleanupHook(DeleteMe, static_cast<void*>(this)); } BaseObject::~BaseObject() { + env_->RemoveCleanupHook(DeleteMe, static_cast<void*>(this)); + if (persistent_handle_.IsEmpty()) { // This most likely happened because the weak callback below cleared it. return; @@ -80,6 +83,12 @@ T* BaseObject::FromJSObject(v8::Local<v8::Object> object) { } +void BaseObject::DeleteMe(void* data) { + BaseObject* self = static_cast<BaseObject*>(data); + delete self; +} + + void BaseObject::MakeWeak() { persistent_handle_.SetWeak( this, |