Skip to content

Commit f4bfde2

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 277bf2b commit f4bfde2

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

@@ -3338,7 +3337,7 @@ inline napi_value ObjectWrap<T>::InstanceSetterCallbackWrapper(
33383337

33393338
template <typename T>
33403339
inline void ObjectWrap<T>::FinalizeCallback(napi_env /*env*/, void* data, void* /*hint*/) {
3341-
T* instance = reinterpret_cast<T*>(data);
3340+
ObjectWrap<T>* instance = static_cast<ObjectWrap<T>*>(data);
33423341
delete instance;
33433342
}
33443343

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)