Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you free a wrapped C++ object when associated Javascript object is garbage collected in V8?

Tags:

V8's documentation explains how to create a Javascript object that wraps a C++ object. The Javascript object holds on to a pointer to a C++ object instance. My question is, let's say you create the C++ object on the heap, how can you get a notification when the Javascript object is collected by the gc, so you can free the heap allocated C++ object?

like image 810
postfuturist Avatar asked Oct 06 '08 06:10

postfuturist


People also ask

How does garbage collector know which objects to free?

When the garbage collector performs a collection, it releases the memory for objects that are no longer being used by the application. It determines which objects are no longer being used by examining the application's roots.

How does JavaScript handle garbage collection?

Some high-level languages, such as JavaScript, utilize a form of automatic memory management known as garbage collection (GC). The purpose of a garbage collector is to monitor memory allocation and determine when a block of allocated memory is no longer needed and reclaim it.

Is there a garbage collector in JavaScript?

There's a background process in the JavaScript engine that is called garbage collector. It monitors all objects and removes those that have become unreachable.

What happens to the memory taken by an object which has been identified as garbage?

As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory.

How does garbage collector work in JavaScript?

Detailed examples to follow. There’s a background process in the JavaScript engine that is called garbage collector. It monitors all objects and removes those that have become unreachable. Here the arrow depicts an object reference. The global variable "user" references the object {name: "John"} (we’ll call it John for brevity).

Why does JavaScript create wrapper objects?

Well, let’s find out why this happens. When we treat a primitive value like it was an object (i.e. by accessing properties and methods), JavaScript creates, under the hood, a wrapper to wrap this value and expose it as an object. The JS engine never reuses a wrapper object, giving them to the garbage collector right after a single use.

How does the JS engine reuse a wrapper object?

The JS engine never reuses a wrapper object, giving them to the garbage collector right after a single use. If you write something like const surname = ‘Freddie Mercury’.substr (8, 7);, you’re not storing a reference to the created wrapper object in the surname variable; actually, what is being assigned is the primitive value itself.

How to make an object eligible for garbage collection in Java?

How to make object eligible for garbage collection in Java? An object is eligible to be garbage collected if its reference variable is lost from the program during execution.Sometimes they are also called unreachable objects. What is reference of an object? The new operator dynamically allocates memory for an object and returns a reference to it.


1 Answers

The trick is to create a Persistent handle (second bullet point from the linked-to API reference: "Persistent handles are not held on a stack and are deleted only when you specifically remove them. ... Use a persistent handle when you need to keep a reference to an object for more than one function call, or when handle lifetimes do not correspond to C++ scopes."), and call MakeWeak() on it, passing a callback function that will do the necessary cleanup ("A persistent handle can be made weak, using Persistent::MakeWeak, to trigger a callback from the garbage collector when the only references to an object are from weak persistent handles." -- that is, when all "regular" handles have gone out of scope and when the garbage collector is about to delete the object).

The Persistent::MakeWeak method signature is:

void MakeWeak(void* parameters, WeakReferenceCallback callback); 

Where WeakReferenceCallback is defined as a pointer-to-function taking two parameters:

typedef void (*WeakReferenceCallback)(Persistent<Object> object,                                       void* parameter); 

These are found in the v8.h header file distributed with V8 as the public API.

You would want the function you pass to MakeWeak to clean up the Persistent<Object> object parameter that will get passed to it when it's called as a callback. The void* parameter parameter can be ignored (or the void* parameter can point to a C++ structure that holds the objects that need cleaning up):

void CleanupV8Point(Persistent<Object> object, void*) {     // do whatever cleanup on object that you're looking for     object.destroyCppObjects(); }  Parameter<ObjectTemplate> my_obj(ObjectTemplate::New());  // when the Javascript part of my_obj is about to be collected // we'll have V8 call CleanupV8Point(my_obj) my_obj.MakeWeak(NULL, &CleanupV8Point); 
like image 191
Max Lybbert Avatar answered Oct 30 '22 13:10

Max Lybbert