I use a c++ wrapper to connect with NodeJS. I use the webp library to read in data (for this example).
1 2 3 4 5 6
// make a copy and cleanup
uint8_t* cpy;
cpy = new uint8_t[length];
memcpy(cpy, webpData, length);
obj.Set(Napi::String::New(env, "buffer"), Napi::ArrayBuffer::New(env, cpy, length));
WebPFree(webpData);
Here is where the magic happens. I make a copy and cleanup webpData, but the NodeJS engine is never aware to garbage collect because of a mistake I think I am making here. So after running my script on 10s of thousands of images, the ram is eaten alive pretty quickly.
The Napi::ArrayBuffer instance does not assume ownership for the data and expects it to be valid for the lifetime of the instance. The data can only be freed once the finalizeCallback is invoked to indicate that the Napi::ArrayBuffer has been released.