Skip to content

Commit 4c7f8ba

Browse files
committed
src: make ObjectWrap dtor virtual
Otherwise, subclasses of `ObjectWrap` would not be deleted correctly by the `delete instance;` line in `FinalizeCallback()`. (This is also just the right thing to do for classes from which subclasses are supposed to be created.)
1 parent 06ddc04 commit 4c7f8ba

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

napi-inl.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2806,11 +2806,10 @@ inline ObjectWrap<T>::ObjectWrap(const Napi::CallbackInfo& callbackInfo) {
28062806
napi_value wrapper = callbackInfo.This();
28072807
napi_status status;
28082808
napi_ref ref;
2809-
T* instance = static_cast<T*>(this);
2810-
status = napi_wrap(env, wrapper, instance, FinalizeCallback, nullptr, &ref);
2809+
status = napi_wrap(env, wrapper, this, FinalizeCallback, nullptr, &ref);
28112810
NAPI_THROW_IF_FAILED_VOID(env, status);
28122811

2813-
Reference<Object>* instanceRef = instance;
2812+
Reference<Object>* instanceRef = this;
28142813
*instanceRef = Reference<Object>(env, ref);
28152814
}
28162815

@@ -3335,7 +3334,7 @@ inline napi_value ObjectWrap<T>::InstanceSetterCallbackWrapper(
33353334

33363335
template <typename T>
33373336
inline void ObjectWrap<T>::FinalizeCallback(napi_env /*env*/, void* data, void* /*hint*/) {
3338-
T* instance = reinterpret_cast<T*>(data);
3337+
ObjectWrap<T>* instance = static_cast<ObjectWrap<T>*>(data);
33393338
instance->_is_in_finalize = true;
33403339
delete instance;
33413340
}

napi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ namespace Napi {
15691569
class ObjectWrap : public Reference<Object> {
15701570
public:
15711571
ObjectWrap(const CallbackInfo& callbackInfo);
1572-
~ObjectWrap();
1572+
virtual ~ObjectWrap();
15731573

15741574
static T* Unwrap(Object wrapper);
15751575

0 commit comments

Comments
 (0)