tondrej / chakracore-delphi

Delphi and Free Pascal bindings and classes for Microsoft's ChakraCore library
https://tondrej.blogspot.com/search/label/chakracore
MIT License
136 stars 34 forks source link

How to use js to release tconsole? #4

Closed pony5551 closed 5 years ago

pony5551 commented 5 years ago

var c = new Console(); //How to release here? delete c;//? c = null;//?

tondrej commented 5 years ago

c = null; works, as well as c = 42;. Just by assigning the variable a different value the previously stored instance's reference count is automatically decreased by ChakraCore, and later when the garbage collector sees 0 reference count it will call the registered finalizer callback which in turn frees the instance.

pony5551 commented 5 years ago

c=null; But this does not release the object!

tondrej commented 5 years ago

c=null; But this does not release the object!

Not immediately - but that's by design. GC releases the object eventually. (It might be a good idea to extend the test suite with a proof.)

If you think you've found a bug please provide a MCVE. Thanks!

From my own experiments it works as expected. All native instances are released, either by the GC at some point during execution of the script or at the latest when the runtime is disposed.

pony5551 commented 5 years ago

I don't see where to call TNativeObject.AddRef and TNativeObject.Release!

In my opinion, is TNativeObject designed as an interface more appropriate? Because now you don't have to call JsDisposeRuntime and all of your new objects won't be thrown!

pony5551 commented 5 years ago

Maybe my idea is not necessarily correct, maybe you have other better way!

pony5551 commented 5 years ago

procedure Main; var DataModule: TDataModuleMain; begin ShowInfo;

DataModule := TDataModuleMain.Create(nil); try DataModule.Execute(ParamStrings); ReadLn; //At this point, new Console() is not released finally DataModule.Free; //When you get here, new Console() is released end; end;

tondrej commented 5 years ago

I'm closing this issue with "works as designed". Please see the new finalizer tests in 48bea58.