Description
- 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.