Skip to content

Fix illegal delegate usage #1328

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

Merged

Conversation

BadSingleton
Copy link
Contributor

@BadSingleton BadSingleton commented Dec 17, 2020

What does this implement/fix? Explain your changes.

Cache the delegates to native code we've created so that when we need to
call that delegate, we don't ask for a delegate from a native pointer
(that is a delegate to managed code.)

Does this close any currently open issues?

This fixes issue #1323 .

Any other comments?

This is needed in order for #1287 to pass the domain reload tests on Mono.

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Add yourself to AUTHORS
  • Updated the CHANGELOG

Cache the delegates to native code we've created so that when we need to
call that delegate, we don't ask for a delegate from a native pointer
(that is a delegate to managed code.)
@BadSingleton
Copy link
Contributor Author

Test failures are the same as the ones in the current master: https://github.com/pythonnet/pythonnet/runs/1571830014

if (!Interop.allocatedThunks.TryGetValue(fp, out d))
{
// Use Marshal.GetDelegateForFunctionPointer<> directly after upgrade the framework
d = Marshal.GetDelegateForFunctionPointer(fp, typeof(T));
Copy link
Member

Choose a reason for hiding this comment

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

Can you also apply the comment here? The generic function was introduced in .NET 4.5.1.

Copy link
Member

Choose a reason for hiding this comment

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

Actually, maybe just remove the comment. You are not writing this into allocatedThunks right now, btw.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I don't cache these because they truly point to unmanaged code. We should only cache the function pointers to managed code.

// We don't cache this delegate because this is a pure delegate ot unmanaged.
d = Marshal.GetDelegateForFunctionPointer<T>(fp);
}
return (T)d;
Copy link
Contributor

Choose a reason for hiding this comment

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

You'd save a couple instructions with:

Delegate d; // no init
if (trygetvalue(out d)) {
  return (T)d;
} else {
  return Marshal.GetDelegate...(); // no cast
}

@BadSingleton BadSingleton requested a review from filmor December 18, 2020 17:06
Comment on lines +45 to +46
Delegate d = null;
if (!Interop.allocatedThunks.TryGetValue(fp, out d))
Copy link
Member

@lostmsu lostmsu Dec 18, 2020

Choose a reason for hiding this comment

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

NIT: you don't need to assign an initial value to d. You could also do TryGetValue(fp, out var d).

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.

4 participants