Bug 465825

Summary: CCW crashes when marshalling arrays
Product: [Mono] Mono: Runtime Reporter: Eberhard Beilharz <eb1>
Component: interopAssignee: Jonathan Chambers <joncham>
Status: RESOLVED FIXED QA Contact: Mono Bugs <mono-bugs>
Severity: Normal    
Priority: P5 - None CC: eb1, forgotten_vxPDddArjq, joncham
Version: SVN   
Target Milestone: ---   
Hardware: Other   
OS: Ubuntu   
Whiteboard:
Found By: --- Services Priority:
Business Priority: Blocker: ---
Marketing QA Status: --- IT Deployment: ---
Attachments: A patch for the cominterop tests that shows the problem
Proposed fix

Description Eberhard Beilharz 2009-01-13 19:56:01 UTC
Created attachment 264847 [details]
A patch for the cominterop tests that shows the problem

Calling a managed method from unmanaged code via COM fails if a C style array is passed as an in parameter.

Example of the IDL:

HRESULT EatCookies(
	[in] int cCookies,
	[in, size_is(cCookies)] int* cookies);

C# implementation of EatCookies:

public void EatCookies(int cCookies, int[] cookies)
{
	Console.Write("Eating {0} cookies: ", cCookies);
	for (int i = 0; i < cCookies; i++)
		Console.Write("{0}, ", cookies[i]);
	Console.WriteLine();
}

Interop defined as:

[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)]
void EatCookies(int cCookies, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)] int[] cookies);

Call EatCookies from C++:

	m_customer->EatCookies(1, cookies);

When running it crashes:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
  at System.String.memcpy4 (System.Byte* dest, System.Byte* src, Int32 size) [0x00000] 
  at System.String.memcpy (System.Byte* dest, System.Byte* src, Int32 size) [0x00000] 
  at (wrapper native-to-managed) IBakery.ICustomer:EatCookies (intptr,int,int[])


Attached are patches for the cominterop tests that demonstrate the problem, and a fix to the CCW marshaling that fixes the crash.
Comment 1 Eberhard Beilharz 2009-01-13 20:01:00 UTC
Created attachment 264850 [details]
Proposed fix
Comment 2 Forgotten User vxPDddArjq 2009-01-13 21:27:51 UTC
Jonathan, could you look at the patch ?
Comment 3 Eberhard Beilharz 2009-06-23 16:35:48 UTC
This seems to be fixed with #136029