Skip to content

Conversation

@spahnke
Copy link
Collaborator

@spahnke spahnke commented May 8, 2018

Hi,

we need to pass references to JavaScript functions to C# and call them from there. So I went ahead and merged the branch jsfunc with the current master, replaced deprecated V8 API calls and added some more tests.

I'm totally new to the V8 API, so could you please review my changes? Also, while debugging the tests I found that I get an Access violation exception while disposing the context. The tests are green though. Any advice on that?

The exception is raised in line 8913 of v8.h

V8_INLINE explicit Locker(Isolate* isolate) { Initialize(isolate); }

the callstack being

>	JavaScript.Net.dll!v8::Locker::Locker(v8::Isolate* isolate) Line 8913	C++
 	JavaScript.Net.dll!Noesis::Javascript::JavascriptContext::Enter(Noesis::Javascript::JavascriptContext^% old_context) Line 370	C++
 	JavaScript.Net.dll!Noesis::Javascript::JavascriptScope::JavascriptScope(Noesis::Javascript::JavascriptContext^ iContext) Line 231	C++
 	JavaScript.Net.dll!Noesis::Javascript::JavascriptFunction::!JavascriptFunction() Line 41	C++

I don't think I can change the existing pull request so I opened a new one.

Thanks!

@oliverbock
Copy link
Contributor

Looks good, though there are conflicts that prevent it being merged. Have you been working form the latest source?

Also, regarding the crash in debugging, did that affect you before your changes, or only afterwards?

@spahnke
Copy link
Collaborator Author

spahnke commented May 9, 2018

Hm, I initially merged with the latest master...I went ahead and rebased onto master cleaning up the initial commits, so the merge should hopefully work now.

{
if(mFuncHandle)
{
JavascriptScope scope(mContext);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the access violation exception: It only occurs if the new JavascriptFunction object is used, either in the Fiddling project, or in the new unit tests - though there you only see the exception when debugging. We suspect that the GC calls the finalizer after the JavascriptContext has been destructed leading to a crash here. Is the JavascriptScope necessary? What does it do exactly? I just took it from the old branch.

Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not be calling or otherwise accessing any v8 object after the context has been destroyed, because they will probably have been disposed by v8 and their memory freed. I think you have these options:

  • Don't do anything in the JavascriptFunction destructor, and allow mFuncHandle to be cleaned up when v8 is cleaned up. (And document the memory leak somewhere.)
  • Set a flag in JavascriptContext when it is disposed, which prevent JavascriptFunction from doing anything in its destructor when that flag is set. This is obviously better because it won't leak memory, but is more complicated.

@spahnke
Copy link
Collaborator Author

spahnke commented May 10, 2018

I added a flag as you suggested and the exceptions are gone now. Let me know if this is OK with you!

Thanks!

@oliverbock
Copy link
Contributor

Looks good to me. Thanks.

@oliverbock oliverbock merged commit cc1d105 into JavascriptNet:master May 11, 2018
@spahnke spahnke deleted the jsfunc branch May 11, 2018 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants