Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9307bb3

Browse files
authored
Fix illegal delegate usage (#1328)
* Fix illegal delegate usageCache the delegates to native code we've created so that when we need tocall that delegate, we don't ask for a delegate from a native pointer(that is a delegate to managed code.)* Use the generic method
1 parent139dc87 commit9307bb3

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

‎src/runtime/interop.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,9 @@ internal static Type GetPrototype(string name)
491491
returnpmap[name]asType;
492492
}
493493

494+
495+
internalstaticDictionary<IntPtr,Delegate>allocatedThunks=newDictionary<IntPtr,Delegate>();
496+
494497
internalstaticThunkInfoGetThunk(MethodInfomethod,stringfuncType=null)
495498
{
496499
Typedt;
@@ -505,6 +508,7 @@ internal static ThunkInfo GetThunk(MethodInfo method, string funcType = null)
505508
}
506509
Delegated=Delegate.CreateDelegate(dt,method);
507510
varinfo=newThunkInfo(d);
511+
allocatedThunks[info.Address]=d;
508512
returninfo;
509513
}
510514

‎src/runtime/managedtype.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ internal static int PyVisit(IntPtr ob, IntPtr visit, IntPtr arg)
178178
{
179179
return0;
180180
}
181-
varvisitFunc=(Interop.ObjObjFunc)Marshal.GetDelegateForFunctionPointer(visit,typeof(Interop.ObjObjFunc));
181+
varvisitFunc=NativeCall.GetDelegate<Interop.ObjObjFunc>(visit);
182182
returnvisitFunc(ob,arg);
183183
}
184184

@@ -196,7 +196,7 @@ internal void CallTypeClear()
196196
{
197197
return;
198198
}
199-
varclearFunc=(Interop.InquiryFunc)Marshal.GetDelegateForFunctionPointer(clearPtr,typeof(Interop.InquiryFunc));
199+
varclearFunc=NativeCall.GetDelegate<Interop.InquiryFunc>(clearPtr);
200200
clearFunc(pyHandle);
201201
}
202202

@@ -214,7 +214,8 @@ internal void CallTypeTraverse(Interop.ObjObjFunc visitproc, IntPtr arg)
214214
{
215215
return;
216216
}
217-
vartraverseFunc=(Interop.ObjObjArgFunc)Marshal.GetDelegateForFunctionPointer(traversePtr,typeof(Interop.ObjObjArgFunc));
217+
vartraverseFunc=NativeCall.GetDelegate<Interop.ObjObjArgFunc>(traversePtr);
218+
218219
varvisiPtr=Marshal.GetFunctionPointerForDelegate(visitproc);
219220
traverseFunc(pyHandle,visiPtr,arg);
220221
}

‎src/runtime/nativecall.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ public static int Int_Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3)
4040
returnd(a1,a2,a3);
4141
}
4242

43-
privatestaticTGetDelegate<T>(IntPtrfp)whereT:Delegate
43+
internalstaticTGetDelegate<T>(IntPtrfp)whereT:Delegate
4444
{
45-
// Use Marshal.GetDelegateForFunctionPointer<> directly after upgrade the framework
46-
return(T)Marshal.GetDelegateForFunctionPointer(fp,typeof(T));
45+
Delegated=null;
46+
if(!Interop.allocatedThunks.TryGetValue(fp,outd))
47+
{
48+
// We don't cache this delegate because this is a pure delegate ot unmanaged.
49+
d=Marshal.GetDelegateForFunctionPointer<T>(fp);
50+
}
51+
return(T)d;
4752
}
4853
}
4954
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp