[Mono-devel-list] Bug with dynamically-created delegates

Marcus mathpup at mylinuxisp.com
Sat Aug 9 20:22:11 EDT 2003


Bugzilla has been down for several days, so I am reporting this here. (I had 
filed a bugzilla report before bugzilla quit operations.)

When I create a subclass of MulticastDelegate in a dynamic assembly, 
instantiate the class, add a delegate, and then attempt to pass it to 
unmanaged code via Marshal.StructureToPtr(), Mono aborts with

** ERROR **: file metadata.c: line 909 (mono_metadata_decode_row_col): 
assertion failed: (col < mono_metadata_table_count (bitfield))
aborting...

The code works correctly under Rotor:

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;


public class Testing
{
    public static void Method(int value)
    {
        Console.WriteLine( "Method( {0} )", value );
    }


    [StructLayout(LayoutKind.Sequential)]
    internal struct DelegateList
    {
        internal Delegate del;
    }


    public static void Main()
    {
        // Create a dynamic assembly and module to contain the
        // subclass of MulticastDelegate that we will create

        AssemblyName asmName = new AssemblyName();
        asmName.Name = "DynamicAssembly";

        AssemblyBuilder asmBuilder =
            AppDomain.CurrentDomain.DefineDynamicAssembly(
                asmName, AssemblyBuilderAccess.Run );

        ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule
( "DynamicModule" );

        TypeBuilder typeBuilder = modBuilder.DefineType( "MyType",
            TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.
Sealed,
            typeof( System.MulticastDelegate ) );

        ConstructorBuilder cb = typeBuilder.DefineConstructor(
            MethodAttributes.Public | MethodAttributes.HideBySig |
            MethodAttributes.RTSpecialName | MethodAttributes.SpecialName,
            CallingConventions.Standard,
            new Type[] { typeof(Object), typeof (IntPtr) } );

        cb.SetImplementationFlags( MethodImplAttributes.Runtime | 
MethodImplAttributes.Managed );

        MethodBuilder mb = typeBuilder.DefineMethod(
            "Invoke",
            MethodAttributes.Public | MethodAttributes.Virtual | 
MethodAttributes.HideBySig,
            typeof(void),
            new Type[] { typeof(int) } );

        mb.SetImplementationFlags( MethodImplAttributes.Runtime | 
MethodImplAttributes.Managed );


        // Create an instance of the delegate type and invoke it -- just to 
test

        Type myDelegateType = typeBuilder.CreateType();
        Delegate d = Delegate.CreateDelegate( myDelegateType, typeof
( Testing ), "Method" );
        d.DynamicInvoke( new object[] { 8 } );



        DelegateList delegateList = new DelegateList();
        delegateList.del = d;
        IntPtr ptr = Marshal.AllocHGlobal( Marshal.SizeOf( delegateList ) );

        // The execption seems to occur at this statement:
        Marshal.StructureToPtr( delegateList, ptr, false );
    }

}



More information about the Mono-devel-list mailing list