-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AccessViolationException #1033
Comments
Ok, I managed to resolve the problem. It seems when you are passing arguments to the function you have to call AddRef() on each JavaScriptValue, otherwise they could be collected before the function is actually called. After execution you can release them. I think it is quite important and it should be added to the documentation. Here you have sample code snippets:
|
Thanks for the suggestion @Havret . You're pretty much right - you need to pin down |
Actually we already had it documented here. Closing the issue. |
Hi, I know this issue has been closed, but I am having some concerns regarding the solution suggested by @Havret. Don't you still have a race condition with the garbage collector between the two following lines? javaScriptFunctionArguments[i + 1] = JavaScriptConverter.FromObject(arguments[i]);
// garbage collection can happen here, collecting the newly created object
javaScriptFunctionArguments[i + 1].AddRef(); Am I missing something here? Morten |
@mortenab You still have a native stack reference to the JavaScriptValue s.t. GC won't collect the object. |
@liminzhu thanks for the reply. So I guess the .NET stack is seen as part of the native stack? If I for example implemented the function call in this way: JavaScriptValue jsStr = JavaScriptValue.FromString("testArg");
var result = javaScriptFunction.CallFunction(new JavaScriptValue[] { JavaScriptValue.Null, jsStr }); I would not need to call Thanks, Morten |
No you're fine as long as the object is still on the C# stack. |
@liminzhu thanks for the clarification. I have created another issue in the samples repo: microsoft/Chakra-Samples#49 |
@liminzhu sorry I have a couple more questions on this... I'm not sure I understand how C# call stack can prevent an object from being collected by JavaScript GC? I had an impression the two GCs are not really aware of each other? Also does JavaScript GC always run continuously on a separate thread? Or does it pause while a host callback function is being executed? |
The C# GC and the ChakraCore GC are not aware of each other, but the ChakraCore GC can walk the native stack and find pointers to things in the ChakraCore recycler and use those as roots. As long as the translation of various The ChakraCore recycler doesn't run continuously (unless it has to due to app behavior), but it does do as much as possible away from the main thread. Most of the "marking" will happen on background threads, then the main thread will be stopped momentarily for a final pass to make sure that there is a consistent state of the world. At this point we also handle some cleanup synchronously, like invoking finalizers. Once that is done, most of the "sweep" of actually returning memory to the recycler to be used later happens on a background thread again. |
Thanks, @MSLaguana. That clarifies it a bit, however after more digging I found this discussion from which it sounds like it might not always be safe to rely on the compiler to actually keep values on the stack, and the recommendation seems to be to still call JsAddRef/JsRelease: #2836 Also I'm not sure if running a .NET host complicates matters even further compared to a native C host... I couldn't find much documentation on how .NET runtime interacts with the native stack and if it does any additional optimizations, but wanted to do more research on this. Should I be worried about this at all? Or is it something that can happen in theory, but shouldn't really happen in practice? |
I believe that the comments in that other issue were around a compiler noticing that a value doesn't seem to be live any more, and replacing it with something else. E.g.
Even though
then it's possible that between creating TL;DR: as long as you don't try to hide your references, and whenever you use heap allocated storage to hold a |
I see, that makes a lot of sense now. As always, thank you for your super promt response, detailed answers and patience while answering my questions :) |
Hi!
When I try recreate Chakra Runtime, create Chakra Context and acquire Context Scope several times I got the following exception when trying to call a function. Here is additional message:
I am using the latest Chakra version with release build (x64).
The text was updated successfully, but these errors were encountered: