Question

Do I need handle_scope if I only want to return a value?

Handle<Value> platformName(const Arguments& a) {
    HandleScope handle_scope;
    return String::New("linux");
}
Was it helpful?

Solution

Presuming that your function always is called from an existing scope, there's no need for you to declare a scope within this function. The choice to include scopes per-function is largely a choice in whether or not to free up memory for garbage collection per-function. You probably don't need that, and should just periodically close out your "primary" scope to allow GC to occur.

If you want to create a new scope in this function, then the correct method of return for your example is:

return handle_scope.Close(String::New("linux"));

In this case, doing so would give you no additional value because there are no local handles created in the scope; there's just the one that you are returning, which would be in the enclosing scope anyway. But if you were creating several local handles in this function, then it could be worthwhile to enclose them in a new scope so they can be GC'ed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top