Skip to content

memleak using worker and objects wrapping c++ instances #38816

Description

@julianjany
  • Version: 16.2.0
  • Platform: Windows 10 x86-64 (most likely platform-independent)
  • Subsystem: Workers

What steps will reproduce the bug?

Sample description

provided reproducer-sample: gist

The main thread continuously instantiates worker-threads.
Inside of these workers an instance of a wrapped C++ object is created (see Wrapping C++ objects).
These wrapped objects are not properly destroyed on worker-shutdown (assuming garbage-collection is not manually triggered before shutdown), causing a memory-leak.

Steps to reproduce the bug locally

  • install node-gyp if not already installed → run: npm i -g node-gyp
  • clone the following gist
  • open the project-root-folder
  • run: node-gyp configure
  • run: node-gyp build
  • execute the example → run: node --expose-gc ./testAddon.js

How often does it reproduce? Is there a required condition?

always. no precondition.

What is the expected behavior?

Proper destruction of the wrapped objects.

expected console-output:

...
worker startup
native: MyObject()
native: ~MyObject()
worker shutdown
...

What do you see instead?

The wrapped objects are not destroyed and cause a memory-leak.

perceived console-output:

...
worker startup
native: MyObject()
worker shutdown
...

The instantiated objects allocate a large integer vector, so the effects of the memory-leak are also easily visible in "Task Manager".

Additional information

The same behavior also applies to wrapped objects instantiated inside the main thread.
There the problem is not as severe because the memory is "handed back" to the OS on process-shutdown.
A C++ developer developing a native addon still expects the destructor of the object to be called at some point, which makes this behavior problematic.

Activity

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

    addonsIssues and PRs related to native addons.c++Issues and PRs that require attention from people who are familiar with C++.docIssues and PRs related to Node.js documentation.memoryIssues and PRs related to Node.js memory management or memory footprint.workerIssues and PRs related to the worker_threads module and Worker API.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions