Skip to content

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

Closed

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

Activity

  1. mhdawson commented on Jan 24, 2025

    @mhdawson
    Member

    @graebm, we discussed in the node-api team meeting today and agree the example should use delete instead of calling ~MyObject(). Would you like to submit a PR to fix that?

  2. graebm commented on Jan 28, 2025

    @graebm
    ContributorAuthor

    I posted a PR, but didn't do any local testing. I leave it in your hands...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions