Bug 20020

Summary: CustomMarshaler in referenced assembly
Product: [Mono] Runtime Reporter: Robert Persson <r.k.persson>
Component: InteropAssignee: Bugzilla <bugzilla>
Status: RESOLVED FIXED    
Severity: normal CC: mono-bugs+mono, mono-bugs+runtime, r.k.persson, vargaz
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Tags: Is this bug a regression?: ---
Last known good build:

Description Robert Persson 2014-05-23 17:57:40 UTC
When calling Marshal.GetFunctionPointerForDelegate for a delegate type declared in a referenced assembly, which is using a custom marshaler, the application crashes with the below info. The code was tested and working under microsoft .NET.

---------------------------------------------------------------------------
* Assertion at marshal.c:5893, condition `mtype != NULL' not met

Stacktrace:

  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegateInternal (System.Delegate) <IL 0x00021, 0xffffffff>
  at System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate (System.Delegate) [0x00011] in /build/buildd/mono-3-opt-3.2.3/mcs/class/corlib/System.Runtime.InteropServices/Marshal.cs:1277
  at monoBugTest.MainClass.Main (string[]) [0x0000f] in /home/robert/monoBugTest/monoBugTest/Program.cs:12
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
----------------------------------------------------------------------------

//Code in main executable:
namespace monoBugTest
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			CallbackDelegate cb = new CallbackDelegate (TestFunction);
			IntPtr i=Marshal.GetFunctionPointerForDelegate (cb); // <= crashes here
		}

		public static void TestFunction(object o) {	}
	}
}

------------------------------------------------------------------------------
//Code in referenced assembly:
namespace externalAsm
{
	[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
	public delegate void CallbackDelegate([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(TestMarshaler))] object sender);



	public class TestMarshaler : ICustomMarshaler
	{
		static TestMarshaler static_instance;

		public IntPtr MarshalManagedToNative(object managedObj)	{return IntPtr.Zero;}

		public object MarshalNativeToManaged(IntPtr pNativeData) {return null;}

		public void CleanUpNativeData(IntPtr pNativeData) {	}

		public void CleanUpManagedData(object managedObj) {	}

		public int GetNativeDataSize() {return -1;}

		public static ICustomMarshaler GetInstance(string cookie)
		{
			if (static_instance == null)
				static_instance = new TestMarshaler();

			return static_instance;
		}

	}
}
Comment 1 Zoltan Varga 2014-06-05 14:48:37 UTC
Fixed in mono master 4385e25d7a9694bc140ec2ab88d4953942fd8057.