I am building an app where there are some dynamically generated buttons in table view cells. Everything is working fine on iPhone 4 & 4S. However after a few repetitions of the workflow, on clicking a button in table, the app crashes without giving any information in the debugger. This is happening consistently. Only once I got an exception with details as:
System.Exception: Failed to marshal the Objective-C object 0x19972ac0 (type: MonoTouch.UIKit.UIControlEventProxy). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'MonoTouch.UIKit.UIControlEventProxy' does not have a constructor that takes one IntPtr argument). at MonoTouch.ObjCRuntime.Runtime.MissingCtor (IntPtr ptr, IntPtr klass, System.Type type, MissingCtorResolution resolution) [0x00046] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pp-Runtime.cs:352 at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject[NSObject] (IntPtr ptr, System.Type type, MissingCtorResolution missingCtorResolution) [0x00000] in <filename unknown>:0 at MonoTouch.ObjCRuntime.Runtime.GetNSObject (IntPtr ptr, System.Type target_type, MissingCtorResolution missingCtorResolution, System.Boolean& created) [0x00073] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pp-Runtime.cs:514 at MonoTouch.ObjCRuntime.Runtime.GetNSObjectWrapped (IntPtr ptr, IntPtr type_ptr, System.Boolean& created) [0x0000c] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pp-Runtime.cs:686 at at (wrapper native-to-managed) MonoTouch.ObjCRuntime.Runtime:GetNSObjectWrapped (intptr,intptr,int&) at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pp-UIApplication.cs:38 at DominosOET.Application.Main (System.String[] args) [0x00008] in /Users/juligupta/TFS Workspace/OET/DominosOET/Main.cs:16
This is happening on a combination of iOS 7 and iPhone 5. Can anyone shed some light on what might be causing this. I am not getting any info in the debugger.
Posts
Most likely the dynamically created buttons were freed by the GC, and thus freeing the event handler as well.
One possible solution is to use a custom UIButton subclass instead of UIButton itself (this works because Xamarin.iOS treats subclasses differently than the framework types themselves). Could you try this and see if it helps?
Thanks for the quick response.
It worked!!!
And the app is running smoothly again. Thanks a lot for your help.
And if you want to use a button on table row?
How do you do this?
@Gaurav Could you please post some code that would be useful for me.