From 66f18ae900b4f550a48be5043ec28e786b8af429 Mon Sep 17 00:00:00 2001 From: "alexander.corrado" Date: Tue, 1 Jun 2010 10:20:23 +0000 Subject: [PATCH] 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 --- CPPPOC/Main.cs | 3 ++- Mono.VisualC.Interop/ABI/VTable.cs | 15 ++++++++------- Mono.VisualC.Interop/ABI/VTableCOM.cs | 2 +- Mono.VisualC.Interop/ABI/VTableManaged.cs | 23 +++++++++-------------- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/CPPPOC/Main.cs b/CPPPOC/Main.cs index fff1a691..ea3cf4e9 100644 --- a/CPPPOC/Main.cs +++ b/CPPPOC/Main.cs @@ -19,7 +19,8 @@ namespace CPPPOC CSimpleClass csc1 = new CSimpleClass(CreateCSimpleSubClass(10)); CSimpleClass csc2 = new CSimpleClass(2); 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.M0(); Console.WriteLine("Managed code got value: {0}", csc1.value); diff --git a/Mono.VisualC.Interop/ABI/VTable.cs b/Mono.VisualC.Interop/ABI/VTable.cs index ee841951..c0c985fc 100644 --- a/Mono.VisualC.Interop/ABI/VTable.cs +++ b/Mono.VisualC.Interop/ABI/VTable.cs @@ -17,8 +17,11 @@ using System.Runtime.InteropServices; namespace Mono.VisualC.Interop.ABI { public abstract class VTable : IDisposable { 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 { get { return Marshal.SizeOf (typeof (IntPtr)); } } @@ -29,16 +32,14 @@ namespace Mono.VisualC.Interop.ABI { // Creates a new VTable public VTable (Delegate[] overrides) { - EntryCount = overrides.Length; - Console.WriteLine ("VTable entry count: {0}", EntryCount); - basePtr = IntPtr.Zero; - vtPtr = IntPtr.Zero; + this.overrides = overrides; + this.basePtr = IntPtr.Zero; + this.vtPtr = IntPtr.Zero; } - public virtual void WriteOverrides (Delegate[] overrides) + protected virtual void WriteOverrides (int offset) { IntPtr vtEntryPtr; - int offset = 0; for (int i = 0; i < EntryCount; i++) { if (overrides [i] != null) // managed override diff --git a/Mono.VisualC.Interop/ABI/VTableCOM.cs b/Mono.VisualC.Interop/ABI/VTableCOM.cs index 1c36b2d0..0384eee9 100644 --- a/Mono.VisualC.Interop/ABI/VTableCOM.cs +++ b/Mono.VisualC.Interop/ABI/VTableCOM.cs @@ -25,7 +25,7 @@ namespace Mono.VisualC.Interop.ABI { select entry).Count(); vtPtr = Marshal.AllocHGlobal ((EntryCount + managedOverrides) * EntrySize); - WriteOverrides (entries); + WriteOverrides (0); } public override MethodInfo PrepareVirtualCall (MethodInfo target, ILGenerator callsite, FieldInfo vtableField, diff --git a/Mono.VisualC.Interop/ABI/VTableManaged.cs b/Mono.VisualC.Interop/ABI/VTableManaged.cs index 3ba7260b..c90cbcef 100644 --- a/Mono.VisualC.Interop/ABI/VTableManaged.cs +++ b/Mono.VisualC.Interop/ABI/VTableManaged.cs @@ -16,31 +16,26 @@ using System.Runtime.InteropServices; namespace Mono.VisualC.Interop.ABI { public class VTableManaged : VTable { - - private Type[] delegateTypes; private ModuleBuilder implModule; public VTableManaged (ModuleBuilder implModule, Delegate[] entries) : base(entries) { this.implModule = implModule; - this.delegateTypes = new Type [EntryCount]; - - for (int i = 0; i < EntryCount; i++) { - if (entries [i] != null) - delegateTypes [i] = entries [i].GetType (); - } + this.vtPtr = Marshal.AllocHGlobal (EntryCount * EntrySize); - vtPtr = Marshal.AllocHGlobal (EntryCount * EntrySize); - WriteOverrides (entries); + WriteOverrides (0); } public override MethodInfo PrepareVirtualCall (MethodInfo target, ILGenerator callsite, FieldInfo vtableField, LocalBuilder native, int vtableIndex) { - if (delegateTypes [vtableIndex] == null) - delegateTypes [vtableIndex] = Util.GetDelegateTypeForMethodInfo (implModule, target); + Type delegateType; + 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 callsite.Emit (OpCodes.Ldarg_0); @@ -60,7 +55,7 @@ namespace Mono.VisualC.Interop.ABI { callsite.Emit (OpCodes.Throw); callsite.MarkLabel (notNull); - return delegateTypes [vtableIndex].GetMethod ("Invoke"); + return delegateType.GetMethod ("Invoke"); }