-
Notifications
You must be signed in to change notification settings - Fork 762
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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.)
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,10 +40,15 @@ public static int Int_Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3) | |
return d(a1, a2, a3); | ||
} | ||
|
||
private static T GetDelegate<T>(IntPtr fp) where T: Delegate | ||
internal static T GetDelegate<T>(IntPtr fp) where T: Delegate | ||
{ | ||
// Use Marshal.GetDelegateForFunctionPointer<> directly after upgrade the framework | ||
return (T)Marshal.GetDelegateForFunctionPointer(fp, typeof(T)); | ||
Delegate d = null; | ||
if (!Interop.allocatedThunks.TryGetValue(fp, out d)) | ||
{ | ||
// Use Marshal.GetDelegateForFunctionPointer<> directly after upgrade the framework | ||
d = Marshal.GetDelegateForFunctionPointer(fp, typeof(T)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, maybe just remove the comment. You are not writing this into There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
return (T)d; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'd save a couple instructions with:
|
||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 doTryGetValue(fp, out var d)
.