0

In my application I'm creating some JS objects, and also on C++ side some objects for running some handling. I would like to delete C++ objects when they are not needed anymore: the corresponding JS object is Garbage Collected.

I'm trying to find that in Cobalt source code/documentation but I cannot find that. I do see ScriptValue::Reference but this seems to be the opposite: Prevent a JS object from being garbage collected by declaring a relation between a JS value and C++ object.

Could someone give some hints how this can be achieved? (getting some callback called in C++ when an object is garbage collected).

meodou
  • 49
  • 4

1 Answers1

0

Any C++ class exposed to JavaScript must inherit (directly or indirectly) script::Wrappable which has a virtual destructor. This destructor will be called when a corresponding JavaScript object is destroyed, either as a result of garbage collection or JS VM teardown.

Usual caveats apply: GC order and timing are non-deterministic. If you need to free critical resources as soon as possible, expose explicit cleanup methods to JavaScript.

See cobalt::dom::Node::~Node() for example of a cleanup performed upon destruction.

mmotorny
  • 320
  • 1
  • 7