Bug 9830 - Unable to deserialize Action<T> delegates using BinaryFormatter
Summary: Unable to deserialize Action<T> delegates using BinaryFormatter
Status: RESOLVED FEATURE
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 6.0.x
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-01-28 15:39 UTC by Bryan Moulton
Modified: 2013-01-29 14:10 UTC (History)
2 users (show)

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


Attachments
BinaryTest.zip (93.43 KB, application/zip)
2013-01-28 15:39 UTC, Bryan Moulton
Details


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 Developer Community or GitHub 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 FEATURE

Description Bryan Moulton 2013-01-28 15:39:26 UTC
Created attachment 3293 [details]
BinaryTest.zip

In reference to case #23761. When deserializing classes with Action<T> delegates the following exception is thrown. Deserialization works on Windows systems and in the simulator.

MonoDevelop 3.1.1
Installation UUID: b3745c1d-d892-4911-8ad3-3a65abfe4732
Runtime:
	Mono 2.10.9 (tarball)
	GTK 2.24.10
	GTK# (2.12.0.0)
	Package version: 210090011
Mono for Android: 4.4.54 (Evaluation)
Android SDK: /Users/thorri/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.1 (API level 7)
		2.2 (API level 8)
		2.3 (API level 10)
		3.1 (API level 12)
		4.0 (API level 14)
Java SDK: /usr
Apple Developer Tools:
	 Xcode 4.5.2 (1847)
	 Build 4G2008a
Monotouch: 6.0.8
Xamarin.Mac: Not Installed
Build information:
	Release ID: 30101000
	Git revision: 5d928ec4f9d5864b4db04a1301b8a8649b43fb9d
	Build date: 2012-12-14 19:11:30+0000
	Xamarin addins: 80f2dcc8fe4ed316b3e77dde496fc33d90305047
Operating System:
	Mac OS X 10.7.4
	Darwin orri-Gestssons-iMac-2.local 11.4.0 Darwin Kernel Version 11.4.0
	    Mon Apr  9 19:32:15 PDT 2012
	    root:xnu-1699.26.8~1/RELEASE_X86_64 x86_64




STACK TRACE:
[ERROR] FATAL UNHANDLED EXCEPTION: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: An exception was thrown by the type initializer for System.Runtime.Remoting.RemotingServices ---> System.NotSupportedException: Linked away
  at System.Runtime.Remoting.RemotingServices..cctor () [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at System.DelegateSerializationHolder+DelegateEntry.DeserializeDelegate (System.Runtime.Serialization.SerializationInfo info) [0x00048] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/DelegateSerializationHolder.cs:77 
  at System.DelegateSerializationHolder..ctor (System.Runtime.Serialization.SerializationInfo info, StreamingContext ctx) [0x00042] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/DelegateSerializationHolder.cs:113 
  at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00109] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:530 
  --- End of inner exception stack trace ---
  at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0011f] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:536 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MethodBase.cs:98 
  at System.Runtime.Serialization.ObjectRecord.LoadData (System.Runtime.Serialization.ObjectManager manager, ISurrogateSelector selector, StreamingContext context) [0x000d7] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.Serialization/ObjectManager.cs:568 
  at System.Runtime.Serialization.ObjectManager.DoFixups () [0x00066] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.Serialization/ObjectManager.cs:80 
  at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (System.IO.BinaryReader reader) [0x0000f] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:145 
  at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders, System.Object& result, System.Runtime.Remoting.Messaging.Header[]& headers) [0x00041] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:110 
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) [0x0007a] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:169 
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:126 
  at Flyware.Performance.Calculator.Runtime.ProgramDescriptorSerializer.FromBinary (System.Byte[] bytes) [0x00043] in /Users/thorri/Projects/Performance Trunk/src/Performance.Calculator/Flyware.Performance.Calculator/Runtime/ProgramDescriptorSerializer.cs:91 
  at Flyware.Performance.Calculator.Runtime.ProgramDescriptorSerializer.FromZip (System.Byte[] bytes) [0x00050] in /Users/thorri/Projects/Performance Trunk/src/Performance.Calculator/Flyware.Performance.Calculator/Runtime/ProgramDescriptorSerializer.cs:205 
  at Flyware.Performance.Calculator.Runtime.ProgramDescriptorSerializer.FromZip (System.String fileName) [0x00000] in /Users/thorri/Projects/Performance Trunk/src/Performance.Calculator/Flyware.Performance.Calculator/Runtime/ProgramDescriptorSerializer.cs:65 
  at Flyware.Performance.Monotouch.ViewControllers.HomeViewController.<ViewDidLoad>m__6 (System.Object sender, System.EventArgs e) [0x0000b] in /Users/thorri/Projects/Performance Trunk/src/Flyware.Performance.Monotouch/Flyware.Performance.Monotouch/VIewControllers/HomeViewController.cs:118 
  at MonoTouch.UIKit.UIControlEventProxy.Activated () [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIControl.cs:30 
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38 
  at Flyware.Performance.Monotouch.Application.Main (System.String[] args) [0x00000] in /Users/thorri/Projects/Performance Trunk/src/Flyware.Performance.Monotouch/Flyware.Performance.Monotouch/Main.cs:17 
Stacktrace:


Native stacktrace:

	0   FlywarePerformanceMonotouch         0x00ac3fd9 mono_handle_native_sigsegv + 244
	1   FlywarePerformanceMonotouch         0x00af6741 sigabrt_signal_handler + 112
	2   libsystem_c.dylib                   0x31712e93 _sigtramp + 42
	3   libsystem_c.dylib                   0x31709123 pthread_kill + 58
	4   libsystem_c.dylib                   0x31745973 abort + 94
	5   FlywarePerformanceMonotouch         0x00a998e7 monoeg_assertion_message + 58
	6   FlywarePerformanceMonotouch         0x00b4fe48 monotouch_throw_monotouch_exception + 0
	7   FlywarePerformanceMonotouch         0x00ac1a55 mono_invoke_unhandled_exception_hook + 92
	8   FlywarePerformanceMonotouch         0x00a7c8c7 mono_thread_abort + 46
	9   FlywarePerformanceMonotouch         0x00ac3caf mono_handle_exception_internal + 2138
	10  FlywarePerformanceMonotouch         0x00ac3d7d mono_handle_exception + 12
	11  FlywarePerformanceMonotouch         0x00af59fd mono_arm_throw_exception + 132
	12  FlywarePerformanceMonotouch         0x00973e60 throw_exception + 68
	13  FlywarePerformanceMonotouch         0x00882bf8 System_Reflection_MethodBase_Invoke_object_object__ + 80
	14  FlywarePerformanceMonotouch         0x0089eca4 System_Runtime_Serialization_ObjectRecord_LoadData_System_Runtime_Serialization_ObjectManager_System_Runtime_Serialization_ISurrogateSelector_System_Runtime_Serialization_StreamingContext + 804
	15  FlywarePerformanceMonotouch         0x0089cc0c System_Runtime_Serialization_ObjectManager_DoFixups + 324
	16  FlywarePerformanceMonotouch         0x008943e0 System_Runtime_Serialization_Formatters_Binary_ObjectReader_ReadNextObject_System_IO_BinaryReader + 108
	17  FlywarePerformanceMonotouch         0x008941f4 System_Runtime_Serialization_Formatters_Binary_ObjectReader_ReadObjectGraph_System_Runtime_Serialization_Formatters_Binary_BinaryElement_System_IO_BinaryReader_bool_object__System_Runtime_Remoting_Messaging_Header___ + 276
	18  FlywarePerformanceMonotouch         0x00890bf4 System_Runtime_Serialization_Formatters_Binary_BinaryFormatter_NoCheckDeserialize_System_IO_Stream_System_Runtime_Remoting_Messaging_HeaderHandler + 408
	19  FlywarePerformanceMonotouch         0x00890a50 System_Runtime_Serialization_Formatters_Binary_BinaryFormatter_Deserialize_System_IO_Stream + 40
	20  FlywarePerformanceMonotouch         0x00615418 Flyware_Performance_Calculator_Runtime_ProgramDescriptorSerializer_FromBinary_byte__ + 804
	21  FlywarePerformanceMonotouch         0x00616524 Flyware_Performance_Calculator_Runtime_ProgramDescriptorSerializer_FromZip_byte__ + 896
	22  FlywarePerformanceMonotouch         0x006150a0 Flyware_Performance_Calculator_Runtime_ProgramDescriptorSerializer_FromZip_string + 132
	23  FlywarePerformanceMonotouch         0x004db894 Flyware_Performance_Monotouch_ViewControllers_HomeViewController__ViewDidLoadm__6_object_System_EventArgs + 236
	24  FlywarePerformanceMonotouch         0x0026dfd4 MonoTouch_UIKit_UIControlEventProxy_Activated + 68
	25  FlywarePerformanceMonotouch         0x00932230 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200
	26  FlywarePerformanceMonotouch         0x00a7ee1f mono_jit_runtime_invoke + 1054
	27  FlywarePerformanceMonotouch         0x00aec5d3 mono_runtime_invoke + 90
	28  FlywarePerformanceMonotouch         0x00a62bdb native_to_managed_trampoline_MonoTouch_UIKit_UIControlEventProxy_Activated + 178
	29  UIKit                               0x358a20a5 <redacted> + 72
	30  UIKit                               0x358a2057 <redacted> + 30
	31  UIKit                               0x358a2035 <redacted> + 44
	32  UIKit                               0x358a18eb <redacted> + 502
	33  UIKit                               0x358a157b <redacted> + 242
	34  UIKit                               0x357ca523 <redacted> + 318
	35  UIKit                               0x357b7801 <redacted> + 380
	36  UIKit                               0x357b711b <redacted> + 6154
	37  GraphicsServices                    0x395f45a3 <redacted> + 590
	38  GraphicsServices                    0x395f41d3 <redacted> + 34
	39  CoreFoundation                      0x37782173 <redacted> + 34
	40  CoreFoundation                      0x37782117 <redacted> + 138
	41  CoreFoundation                      0x37780f99 <redacted> + 1384
	42  CoreFoundation                      0x376f3ebd CFRunLoopRunSpecific + 356
	43  CoreFoundation                      0x376f3d49 CFRunLoopRunInMode + 104
	44  GraphicsServices                    0x395f32eb GSEventRunModal + 74
	45  UIKit                               0x3580b2f9 UIApplicationMain + 1120
	46  FlywarePerformanceMonotouch         0x0029b73c wrapper_managed_to_native_MonoTouch_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 240
	47  FlywarePerformanceMonotouch         0x004d4a40 Flyware_Performance_Monotouch_Application_Main_string__ + 152
	48  FlywarePerformanceMonotouch         0x00932230 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 200
	49  FlywarePerformanceMonotouch         0x00a7ee1f mono_jit_runtime_invoke + 1054
	50  FlywarePerformanceMonotouch         0x00aec5d3 mono_runtime_invoke + 90
	51  FlywarePerformanceMonotouch         0x00aef33b mono_runtime_exec_main + 306
	52  FlywarePerformanceMonotouch         0x00af2c5f mono_runtime_run_main + 482
	53  FlywarePerformanceMonotouch         0x00a96d33 mono_jit_exec + 94
	54  FlywarePerformanceMonotouch         0x00b3e4b4 main + 2236
	55  libdyld.dylib                       0x39a8db20 <redacted> + 0

=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================
Comment 1 Sebastien Pouliot 2013-01-29 14:10:51 UTC
The code is trying to serialize a function pointer (that's that the Action<T> is) and that goes down into the remoting infrastructure. Remoting is not supported in MonoTouch. However the application is not really doing remoting (it just shares code with it).

You can either:

1. add --nolinkaway to the project options. That will instruct the linker not to remove the (mostly unworking) remoting code from mscorlib.dll. That will result in a larger application and, while it solve _this_ case won't make removing (or similar features) work.

ref: http://spouliot.wordpress.com/2011/11/01/linker-vs-linked-away-exceptions/

2. (my preferred) Do not serialize the method pointer. Implement ISerializable and the serialization constructor and serialize the `action` parameter. That makes serialization is lot faster (native type) and does not require extra code (that --nolinkaway would include) and is more future-proof (since further optimization in the runtime could break #1).

The final code for MyActionClass.cs would look like:

using System;
using System.Runtime.Serialization;

namespace BinaryTest
{
	[Serializable]
	public class MyActionClass : ISerializable
	{
		Action<string> _doItAction;

		public MyActionClass (int action)
		{
			Initialize (action);
		}

		protected MyActionClass (SerializationInfo info, StreamingContext context)
		{
			Initialize ((int) info.GetValue ("action", typeof (int)));
		}

		void Initialize (int action)
		{
			if (action == 0)
				_doItAction = ActionZero;
			else
				_doItAction = ActionOne;
		}

		public void GetObjectData (SerializationInfo info, StreamingContext context)
		{
			info.AddValue ("action", _doItAction == ActionZero ? 0 : 1);
		}

		private void ActionZero(string action)
		{
			Console.WriteLine ("From Action Zero:"+ action);
		}

		private void ActionOne(string action)
		{
			Console.WriteLine ("From Action One:"+ action);
		}

		public void DoIt(string action)
		{
			_doItAction(action);
		}
	}
}