Skip to content

Memory leak in 1-getting-started/6_object_wrap ? #580

Closed
@graebm

Description

@graebm

MyObject* obj = new MyObject(value);
obj->env_ = env;
status = napi_wrap(env,
jsthis,
reinterpret_cast<void*>(obj),
MyObject::Destructor,
nullptr, // finalize_hint
&obj->wrapper_);

The code above allocates memory for MyObject via new (which allocates and calls MyObject::MyObject()), but I don't see where that memory is ever freed?

Here's the finalizer...

void MyObject::Destructor(napi_env env,
void* nativeObject,
void* /*finalize_hint*/) {
reinterpret_cast<MyObject*>(nativeObject)->~MyObject();
}

I believe that finalizer code should be like:

  MyObject* obj = reinterpret_cast<MyObject*>(nativeObject);
  delete obj;

delete will call MyObject::~MyObject() AND free the memory

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions