Bug 20020 - CustomMarshaler in referenced assembly
Summary: CustomMarshaler in referenced assembly
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: Interop ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-05-23 17:57 UTC by Robert Persson
Modified: 2014-06-05 14:48 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

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.