1

In v8.h, the in-source documentation for Persistent::SetWeak says:

As always, GC-based finalization should *not* be relied upon for any critical form of resource management!

This suggests that if it is a critical resource, then you can't manage it with the SetWeak callback mechanism. So what's the alternative for critical resource management in JavaScript?

If I create a mechanism that allows me to create a JavaScript object that is associated with some native C++-managed object, do I have to add a mechanism for explicitly manually releasing that resource via JavaScript?

so, for example, instead of:

let criticalResource = allocateCriticalResource();
doSomethingWith(criticalResource);
criticalResource = null; // this is clearly not enough

...is the alternative to do:

let criticalResource = allocateCriticalResource();
doSomethingWith(criticalResource);
criticalResource.dispose(); // manually dispose the critical resource.

After disposing, the criticalResource JavaScript object would still exist, but it would just an empty shell with no native critical resource attached to it any longer. This obviously puts all the burden on the script developer to ensure the object is not leaked - so that's undesirable.

Or is there some other technique? A better way?

Wyck
  • 10,311
  • 6
  • 39
  • 60
  • 1
    Yes, you have to add that kind of mechanism. There are ways to design APIs that can make forgetting to dispose of a resource harder; one example is http://bluebirdjs.com/docs/api/promise.using.html. – Ry- Jun 24 '20 at 17:46
  • @Ry- if you wrote an answer to that effect, I would mark it as such. – Wyck Jun 25 '20 at 15:37

0 Answers0