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

Exception of new object in multithreading #5

Closed pony5551 closed 5 years ago

pony5551 commented 5 years ago

I uploaded a file!

ThreadDemo.zip

6

{$DEFINE SScript2} is normal!

tondrej commented 5 years ago

I can confirm an access violation. Thanks for finding this, I'll post a new issue with a MCVE as there's too much unrelated code in your example. The issue can be reproduced regardless of multi-threading.

BTW this code is wrong: RegisterMethod(AInstance, 'free', @TConsole.Free);

Any method you wish to register as a native callback must have the specific signature: function(Args: PJsValueRef; ArgCount: Word): JsValueRef of object;

(as shown in the TConsole example).

I should probably make it clear in a comment or wiki.

pony5551 commented 5 years ago

Sorry, I used it wrong

RegisterMethod (AInstance, 'free' @ TConsole. Free);

tondrej commented 5 years ago

Sorry, I used it wrong

RegisterMethod (AInstance, 'free' @ TConsole. Free);

No problem, and thanks for reporting.

tondrej commented 5 years ago

Constructors of projected classes must be addref-ed to prevent them from being garbage-collected. Thank you for finding and reporting it.

tondrej commented 5 years ago

See TestFinalizer4 for a minimal example to reproduce the problem: simply call the script in a loop long enough until GC releases the constructor - . The next attempt to call the constructor will cause an access violation.

pony5551 commented 5 years ago

You may have to submit an issue to chakracore using c++

tondrej commented 5 years ago

Sorry for being unclear. The issue was on the chakracore-delphi side (Pascal), and it's fixed now.

pony5551 commented 5 years ago

I see!thank you!

tondrej commented 5 years ago

Welcome and thank you for your help.