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?