-
-
Notifications
You must be signed in to change notification settings - Fork 477
[QUESTION] How does the finalizer actually work? #1212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is very problematic especially when we have a large C++ object I create a simple reproducer that simulates my issue. C++ code#include <cstdio>
#include <cstdlib>
#include <napi.h>
static void MyDeleter(Napi::Env env, int *my_ptr)
{
printf("MyDeleter is called!\n");
delete[] my_ptr;
}
static Napi::Value Create(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Napi::Object obj = Napi::Object::New(env);
obj["test"] = Napi::String::New(env, "test_string");
obj.AddFinalizer(MyDeleter, new int[1000]);
return obj;
}
static Napi::Object Init(Napi::Env env, Napi::Object exports)
{
exports["create"] = Napi::Function::New(env, Create);
return exports;
}
NODE_API_MODULE(chnet, Init); NodeJS codeconst chnet = require("bindings")("chnet");
while (1) {
let obj = chnet.create();
if (obj.test != "test_string") {
console.log("invalid!");
}
} The Please fix the finalizer API. The finalizer is useless if it's only |
OK, it seems I misunderstand how the GC works here. I took a look at #917. |
Hi @ammarfaizi2 , The finalizer gets ran when the GC decides it needs to run due to running on limited memory. The only way to forcibly run the GC is to start node with Does this answer your questions? Let us know if you need additional assistance. Thanks, Kevin |
Hi @ammarfaizi2 , @vmoroz clarified in today's Node API meeting that the finalizers do not actually run when the GC runs, but is scheduled to run via |
Hi, Thanks for the response. What is |
Hi @ammarfaizi2 ,
|
How does the finalizer actually work?
I have created many objects with:
But the finalizer is only called when my program exits. I want to make
the NodeJS call
MyDeleter()
when the object lifetime ends.The text was updated successfully, but these errors were encountered: