Browse Source

Fixed issue where a DynamicMethod was being collected when it shouldn't have been.

Turns out that the Console.WriteLine was just putting off the inevitable.

git-svn-id: https://mono-soc-2010.googlecode.com/svn/trunk/cppinterop@18 a470b8cb-0e6f-1642-1b45-71e107334c4b
pull/1/head
alexander.corrado 15 years ago
parent
commit
66f18ae900
  1. 3
      CPPPOC/Main.cs
  2. 15
      Mono.VisualC.Interop/ABI/VTable.cs
  3. 2
      Mono.VisualC.Interop/ABI/VTableCOM.cs
  4. 23
      Mono.VisualC.Interop/ABI/VTableManaged.cs

3
CPPPOC/Main.cs

@ -19,7 +19,8 @@ namespace CPPPOC
CSimpleClass csc1 = new CSimpleClass(CreateCSimpleSubClass(10)); CSimpleClass csc1 = new CSimpleClass(CreateCSimpleSubClass(10));
CSimpleClass csc2 = new CSimpleClass(2); CSimpleClass csc2 = new CSimpleClass(2);
try { try {
//TODO: This still calls managed V0 on CSimpleClass first, even though it's an
// instance of a more-derived class.
csc1.V0(25, 50); csc1.V0(25, 50);
csc1.M0(); csc1.M0();
Console.WriteLine("Managed code got value: {0}", csc1.value); Console.WriteLine("Managed code got value: {0}", csc1.value);

15
Mono.VisualC.Interop/ABI/VTable.cs

@ -17,8 +17,11 @@ using System.Runtime.InteropServices;
namespace Mono.VisualC.Interop.ABI { namespace Mono.VisualC.Interop.ABI {
public abstract class VTable : IDisposable { public abstract class VTable : IDisposable {
protected IntPtr basePtr, vtPtr; protected IntPtr basePtr, vtPtr;
protected Delegate[] overrides;
public virtual int EntryCount { get; protected set; } public virtual int EntryCount {
get { return overrides.Length; }
}
public virtual int EntrySize { public virtual int EntrySize {
get { return Marshal.SizeOf (typeof (IntPtr)); } get { return Marshal.SizeOf (typeof (IntPtr)); }
} }
@ -29,16 +32,14 @@ namespace Mono.VisualC.Interop.ABI {
// Creates a new VTable // Creates a new VTable
public VTable (Delegate[] overrides) public VTable (Delegate[] overrides)
{ {
EntryCount = overrides.Length; this.overrides = overrides;
Console.WriteLine ("VTable entry count: {0}", EntryCount); this.basePtr = IntPtr.Zero;
basePtr = IntPtr.Zero; this.vtPtr = IntPtr.Zero;
vtPtr = IntPtr.Zero;
} }
public virtual void WriteOverrides (Delegate[] overrides) protected virtual void WriteOverrides (int offset)
{ {
IntPtr vtEntryPtr; IntPtr vtEntryPtr;
int offset = 0;
for (int i = 0; i < EntryCount; i++) { for (int i = 0; i < EntryCount; i++) {
if (overrides [i] != null) // managed override if (overrides [i] != null) // managed override

2
Mono.VisualC.Interop/ABI/VTableCOM.cs

@ -25,7 +25,7 @@ namespace Mono.VisualC.Interop.ABI {
select entry).Count(); select entry).Count();
vtPtr = Marshal.AllocHGlobal ((EntryCount + managedOverrides) * EntrySize); vtPtr = Marshal.AllocHGlobal ((EntryCount + managedOverrides) * EntrySize);
WriteOverrides (entries); WriteOverrides (0);
} }
public override MethodInfo PrepareVirtualCall (MethodInfo target, ILGenerator callsite, FieldInfo vtableField, public override MethodInfo PrepareVirtualCall (MethodInfo target, ILGenerator callsite, FieldInfo vtableField,

23
Mono.VisualC.Interop/ABI/VTableManaged.cs

@ -16,31 +16,26 @@ using System.Runtime.InteropServices;
namespace Mono.VisualC.Interop.ABI { namespace Mono.VisualC.Interop.ABI {
public class VTableManaged : VTable { public class VTableManaged : VTable {
private Type[] delegateTypes;
private ModuleBuilder implModule; private ModuleBuilder implModule;
public VTableManaged (ModuleBuilder implModule, Delegate[] entries) : base(entries) public VTableManaged (ModuleBuilder implModule, Delegate[] entries) : base(entries)
{ {
this.implModule = implModule; this.implModule = implModule;
this.delegateTypes = new Type [EntryCount]; this.vtPtr = Marshal.AllocHGlobal (EntryCount * EntrySize);
for (int i = 0; i < EntryCount; i++) {
if (entries [i] != null)
delegateTypes [i] = entries [i].GetType ();
}
vtPtr = Marshal.AllocHGlobal (EntryCount * EntrySize); WriteOverrides (0);
WriteOverrides (entries);
} }
public override MethodInfo PrepareVirtualCall (MethodInfo target, ILGenerator callsite, FieldInfo vtableField, public override MethodInfo PrepareVirtualCall (MethodInfo target, ILGenerator callsite, FieldInfo vtableField,
LocalBuilder native, int vtableIndex) LocalBuilder native, int vtableIndex)
{ {
if (delegateTypes [vtableIndex] == null) Type delegateType;
delegateTypes [vtableIndex] = Util.GetDelegateTypeForMethodInfo (implModule, target); if (overrides [vtableIndex] != null)
delegateType = overrides [vtableIndex].GetType ();
else
delegateType = Util.GetDelegateTypeForMethodInfo (implModule, target);
MethodInfo getDelegate = typeof (VTableManaged).GetMethod ("GetDelegateForNative").MakeGenericMethod (delegateTypes [vtableIndex]); MethodInfo getDelegate = typeof (VTableManaged).GetMethod ("GetDelegateForNative").MakeGenericMethod (delegateType);
// load this._vtable // load this._vtable
callsite.Emit (OpCodes.Ldarg_0); callsite.Emit (OpCodes.Ldarg_0);
@ -60,7 +55,7 @@ namespace Mono.VisualC.Interop.ABI {
callsite.Emit (OpCodes.Throw); callsite.Emit (OpCodes.Throw);
callsite.MarkLabel (notNull); callsite.MarkLabel (notNull);
return delegateTypes [vtableIndex].GetMethod ("Invoke"); return delegateType.GetMethod ("Invoke");
} }

Loading…
Cancel
Save