diff options
author | Anna Henningsen <anna@addaleax.net> | 2017-09-09 22:29:08 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-05-10 14:15:17 +0200 |
commit | 61fd027096c0416a6e9bbe3ee7b7edb4c180725a (patch) | |
tree | e64998f427bfaa959448f7e28223b9d10f01e5eb /src/base_object-inl.h | |
parent | 1db0039c505c6081f1d0d84cc24133ed6659e539 (diff) | |
download | android-node-v8-61fd027096c0416a6e9bbe3ee7b7edb4c180725a.tar.gz android-node-v8-61fd027096c0416a6e9bbe3ee7b7edb4c180725a.tar.bz2 android-node-v8-61fd027096c0416a6e9bbe3ee7b7edb4c180725a.zip |
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 786e1f26b4..3bd854639b 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, |