Bug 25853 - [Unified] UIKit.UICollectionView.DequeueReusableCell causes TargetInvocationException
Summary: [Unified] UIKit.UICollectionView.DequeueReusableCell causes TargetInvocationE...
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 7.2.5
Hardware: PC Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-01-08 18:17 UTC by Frank A. Krueger
Modified: 2015-01-20 16:12 UTC (History)
3 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 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 NOT_REPRODUCIBLE

Description Frank A. Krueger 2015-01-08 18:17:53 UTC
I am converting code that works fine on MonoTouch to Unified. 

This innocuous code (DocumentThumbnailsView.cs:1179):

var c = (SortThumbnailCell)collectionView.DequeueReusableCell (DocumentThumbnailsView.SortId, indexPath);

causes this mysterious error. I have tried putting [Preserve] all over my custom cell class, but it still fails. I have also verified that the class is registered before Dequeue is called.

Unhandled managed exception: Exception has been thrown by the target of an invocation. (System.Reflection.TargetInvocationException)
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00016] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:543
at System.Reflection.MonoCMethod.DoInvoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00095] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:528
at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:556
at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/ConstructorInfo.cs:62
at ObjCRuntime.Runtime.ConstructNSObject[UICollectionReusableView] (IntPtr ptr, System.Type type, MissingCtorResolution missingCtorResolution) [0x00000] in <filename unknown>:0
at ObjCRuntime.Runtime.GetNSObject[UICollectionReusableView] (IntPtr ptr) [0x00000] in <filename unknown>:0
at UIKit.UICollectionView.DequeueReusableCell (Foundation.NSString reuseIdentifier, Foundation.NSIndexPath indexPath) [0x0005f] in /Developer/MonoTouch/Source/monotouch/src/build/native/UIKit/UICollectionView.g.cs:160
at Praeclarum.UI.DocumentThumbnailsViewDataSource.GetSortCell (UIKit.UICollectionView collectionView, Foundation.NSIndexPath indexPath) [0x0000f] in /Users/fak/Dropbox/Projects/Praeclarum/Praeclarum.iOS/UI/DocumentThumbnailsView.cs:1179
at Praeclarum.UI.DocumentThumbnailsViewDataSource.GetCell (UIKit.UICollectionView collectionView, Foundation.NSIndexPath indexPath) [0x00017] in /Users/fak/Dropbox/Projects/Praeclarum/Praeclarum.iOS/UI/DocumentThumbnailsView.cs:1132
at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:62
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:45
at Circuit.Touch.Application.Main (System.String[] args) [0x00008] in /Users/fak/Dropbox/Projects/Circuit/CircuitTouch/Main.cs:14


Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0:
0   libsystem_kernel.dylib        	0x319c0b38 __psynch_cvwait + 24
1   libsystem_pthread.dylib       	0x31a3d3f5 _pthread_cond_wait + 517
2   libsystem_pthread.dylib       	0x31a3e2d9 pthread_cond_wait + 37
3   iCircuit                      	0x00b4c611 suspend_current (debugger-agent.c:3053)
4   iCircuit                      	0x00b4c7d1 suspend_current (debugger-agent.c:6835)
5   iCircuit                      	0x00b4aa4b process_event (debugger-agent.c:3931)
6   iCircuit                      	0x00b4ae4b process_single_step_inner (debugger-agent.c:5090)
7   iCircuit                      	0x00b4ebd7 process_signal_event (debugger-agent.c:4892)
8   iCircuit                      	0x00b4ab31 process_single_step (debugger-agent.c:5095)
9   ???                           	0x18974bec 0 + 412568556
10  iCircuit                      	0x003a07cc wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 196
11  iCircuit                      	0x00b703cd mono_jit_runtime_invoke (mini.c:6724)
12  iCircuit                      	0x00bb7b17 mono_runtime_invoke (object.c:2842)
13  iCircuit                      	0x00bbbad7 mono_runtime_invoke_array (object.c:4297)
14  iCircuit                      	0x00b960c9 ves_icall_InternalInvoke (icall.c:2858)
15  iCircuit                      	0x002a88d0 wrapper_managed_to_native_System_Reflection_MonoCMethod_InternalInvoke_System_Reflection_MonoCMethod_object_object___System_Exception_ (MonoMethod.cs:956)
16  iCircuit                      	0x002a8c10 System_Reflection_MonoCMethod_InternalInvoke_object_object__ (MonoMethod.cs:1073)
17  iCircuit                      	0x002a8b38 System_Reflection_MonoCMethod_DoInvoke_object_System_Reflection_BindingFlags_System_Reflection_Binder_object___System_Globalization_CultureInfo (MonoMethod.cs:1032)
18  iCircuit                      	0x002a8cdc System_Reflection_MonoCMethod_Invoke_System_Reflection_BindingFlags_System_Reflection_Binder_object___System_Globalization_CultureInfo (MonoMethod.cs:1111)
19  iCircuit                      	0x002a2928 System_Reflection_ConstructorInfo_Invoke_object__ (ConstructorInfo.cs:123)
20  iCircuit                      	0x0018885c ObjCRuntime_Runtime_ConstructNSObject_T_intptr_System_Type_ObjCRuntime_Runtime_MissingCtorResolution (Runtime.cs:1157)
21  iCircuit                      	0x00189064 ObjCRuntime_Runtime_GetNSObject_T_intptr (Runtime.cs:1345)
22  iCircuit                      	0x001b56e4 UIKit_UICollectionView_DequeueReusableCell_Foundation_NSString_Foundation_NSIndexPath (UICollectionView.g.cs:311)
23  iCircuit                      	0x008ac568 Praeclarum_UI_DocumentThumbnailsViewDataSource_GetSortCell_UIKit_UICollectionView_Foundation_NSIndexPath (DocumentThumbnailsView.cs:2358)
24  iCircuit                      	0x008ab8b4 Praeclarum_UI_DocumentThumbnailsViewDataSource_GetCell_UIKit_UICollectionView_Foundation_NSIndexPath (DocumentThumbnailsView.cs:2263)
25  iCircuit                      	0x003a07cc wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 196
26  iCircuit                      	0x00b703cd mono_jit_runtime_invoke (mini.c:6724)
27  iCircuit                      	0x00bb7b17 mono_runtime_invoke (object.c:2842)
28  iCircuit                      	0x009ef421 native_to_managed_trampoline_24 (registrar.m:868)
29  iCircuit                      	0x009faeb5 -[Praeclarum_UI_DocumentThumbnailsViewDataSource collectionView:cellForItemAtIndexPath:] (registrar.m:5844)
30  UIKit                         	0x2707e8f1 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 249
31  UIKit                         	0x2707cfbd -[UICollectionView _updateVisibleCellsNow:] + 3393
32  UIKit                         	0x27078c05 -[UICollectionView layoutSubviews] + 237
33  UIKit                         	0x2701f24b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 511
34  QuartzCore                    	0x26a47a09 -[CALayer layoutSublayers] + 133
35  QuartzCore                    	0x26a433e1 CA::Layer::layout_if_needed(CA::Transaction*) + 357
36  QuartzCore                    	0x26a43269 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 13
37  QuartzCore                    	0x26a42c4d CA::Context::commit_transaction(CA::Transaction*) + 221
38  QuartzCore                    	0x26a42a51 CA::Transaction::commit() + 321
39  UIKit                         	0x27283051 -[UIApplication _reportMainSceneUpdateFinished:] + 41
40  UIKit                         	0x27283df9 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2509
41  UIKit                         	0x2728e205 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke + 33
42  UIKit                         	0x27282215 -[UIApplication workspaceDidEndTransaction:] + 129
43  FrontBoardServices            	0x2a2aa0cf __31-[FBSSerialQueue performAsync:]_block_invoke + 11
44  CoreFoundation                	0x23b25d7b __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 11
45  CoreFoundation                	0x23b2503d __CFRunLoopDoBlocks + 213
46  CoreFoundation                	0x23b237bf __CFRunLoopRun + 759
47  CoreFoundation                	0x23a713bd CFRunLoopRunSpecific + 473
48  CoreFoundation                	0x23a711cf CFRunLoopRunInMode + 103
49  UIKit                         	0x270861bb -[UIApplication _run] + 555
50  UIKit                         	0x27080f9d UIApplicationMain + 1437
51  iCircuit                      	0x0021ce18 wrapper_managed_to_native_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 268
52  iCircuit                      	0x001b0fd4 UIKit_UIApplication_Main_string___intptr_intptr (UIApplication.cs:122)
53  iCircuit                      	0x001b0f94 UIKit_UIApplication_Main_string___string_string (UIApplication.cs:88)
54  iCircuit                      	0x000336dc Circuit_Touch_Application_Main_string__ (Main.cs:26)
55  iCircuit                      	0x003a07cc wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 196
56  iCircuit                      	0x00b703cd mono_jit_runtime_invoke (mini.c:6724)
57  iCircuit                      	0x00bb7b17 mono_runtime_invoke (object.c:2842)
58  iCircuit                      	0x00bbb027 mono_runtime_exec_main (object.c:4099)
59  iCircuit                      	0x00bbae65 mono_runtime_run_main (object.c:3725)
60  iCircuit                      	0x00b5a365 mono_jit_exec (driver.g.c:1008)
61  iCircuit                      	0x00c1d9bc xamarin_main (monotouch-main.m:400)
62  iCircuit                      	0x009ff559 main (main.armv7.m:55)
63  libdyld.dylib                 	0x318faaad start + 1
Comment 1 Sebastien Pouliot 2015-01-08 23:28:14 UTC
It does not look like something missing - we throw our own (different) exception in such case. In doubt it's generally quicker is to disable the linker for a build (to be 100% sure).

The stack trace seems to indicate the constructor, UICollectionReusableView .ctor(IntPtr), was found and called - but it's execution throwed an exception. Since that's done with reflection what is seen is wrapped inside a `TargetInvocationException` (and the inner exception was not printed).

I'll try to duplicate this tomorrow - but t would be useful if you could put a try/catch around this and look at the InnerException. Also knowing the type hierarchy of SortThumbnailCell would be useful too.

late night self notes:
* This API used (in classic) to return `NSObject` instead of `UICollectionReusableView`;
* Apple states that "This class is intended to be subclassed." [1]
* It might be returning a special internal Apple type that is not in the (documented) hierarchy

[1] https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionReusableView_class/index.html
Comment 2 Sebastien Pouliot 2015-01-09 15:42:17 UTC
I could not duplicate it.

Right now my best guess is that SortThumbnailCell initialization is throwing an exception (any number of reason) and that's the cause of the TIE. The InnerException would tell us...
Comment 3 Frank A. Krueger 2015-01-20 16:12:54 UTC
I can't duplicate it either on more recent builds.

Sorry for the trouble.