Bug 20916 - Get UIKitThreadAccessException when unregistering events
Summary: Get UIKitThreadAccessException when unregistering events
Status: RESOLVED INVALID
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 7.2.4
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-06-26 16:09 UTC by Bill
Modified: 2014-06-30 14:14 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 INVALID

Description Bill 2014-06-26 16:09:11 UTC
Using Xamarin.iOS 7.2.4.4 we are getting UIKitThreadAccessExceptions when just simply unregistering events from UIKit components.

We never used to have this problem before and we do it quite often... including in Dispose methods. Right now we have to go to the UI thread just to do something like button.clicked -= onclicked.

Here is an example stack trace:

Unhandled Exception:
MonoTouch.UIKit.UIKitThreadAccessException: UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.
  at MonoTouch.UIKit.UIApplication.EnsureUIThread () [0x00019] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:49 
  at MonoTouch.UIKit.UIControl.RemoveTarget (MonoTouch.Foundation.NSObject target, MonoTouch.ObjCRuntime.Selector sel, UIControlEvent events) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/UIControl.g.cs:233 
  at MonoTouch.UIKit.UIControl.RemoveTarget (System.EventHandler notification, UIControlEvent events) [0x0004a] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIControl.cs:89 
  at MonoTouch.UIKit.UIControl.remove_TouchDown (System.EventHandler value) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIControl.cs:101 
  at Xactimate.iOS.UI.HomeScreen.LoadMoreView.CleanUpEvents () [0x00013] in /Users/i65781/sdGit/xactimate.ios/Xactimate.iOS/Xactimate.iOS/UI/HomeScreen/LoadMoreView.cs:84 
  at Xactimate.iOS.UI.HomeScreen.BaseLoadMoreProjectsTableViewController.CleanupLoadMoreView (Boolean disposing) [0x0001a] in /Users/i65781/sdGit/xactimate.ios/Xactimate.iOS/Xactimate.iOS/UI/HomeScreen/BaseLoadMoreProjectsTableViewController.cs:170 
  at Xactimate.iOS.UI.HomeScreen.BaseLoadMoreProjectsTableViewController.Dispose (Boolean disposing) [0x00003] in /Users/i65781/sdGit/xactimate.ios/Xactimate.iOS/Xactimate.iOS/UI/HomeScreen/BaseLoadMoreProjectsTableViewController.cs:181 
  at Xactimate.iOS.UI.HomeScreen.CloudProjectsTableViewController.Dispose (Boolean disposing) [0x0003f] in /Users/i65781/sdGit/xactimate.ios/Xactimate.iOS/Xactimate.iOS/UI/HomeScreen/CloudProjectsTableViewController.cs:364 
  at MonoTouch.Foundation.NSObject.Dispose () [0x00000] in /Developer/MonoTouch/Source/maccore/src/Foundation/NSObject2.cs:119 
  at Xactimate.iOS.Logic.MemoryUtils.Dispose (IDisposable disposable) [0x00009] in /Users/i65781/sdGit/xactimate.ios/Xactimate.iOS/Xactimate.iOS.UILogic/MemoryUtils.cs:20 
  at Xactimate.iOS.UI.HomeScreen.CollapsableSplitViewController.Dispose (Boolean disposing) [0x00012] in /Users/i65781/sdGit/xactimate.ios/Xactimate.iOS/Xactimate.iOS/UI/HomeScreen/CollapsableSplitViewController.cs:251 
  at MonoTouch.Foundation.NSObject.Finalize () [0x00000] in /Developer/MonoTouch/Source/maccore/src/Foundation/NSObject2.cs:115
Comment 1 Rolf Bjarne Kvinge [MSFT] 2014-06-27 02:48:29 UTC
You're doing managed cleanup as a result of a finalizer executing, this breaks with the IDisposable guidelines.

In your Dispose methods, you need to check the 'disposing' flag, and if it's false, don't do any managed cleanup.
Comment 2 Bill 2014-06-30 14:05:28 UTC
You are correct about the above stack trace. However, Putting it outside of the "disposing" flag still causes the same problem.

Regardless removing a target shouldn't have anything to do with causing a UIKit inconsistency problem.

New stack trace:

Unhandled Exception:
MonoTouch.UIKit.UIKitThreadAccessException: UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.
  at MonoTouch.UIKit.UIApplication.EnsureUIThread () [0x00019] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:49 
  at MonoTouch.UIKit.UIControl.RemoveTarget (MonoTouch.Foundation.NSObject target, MonoTouch.ObjCRuntime.Selector sel, UIControlEvent events) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/UIControl.g.cs:233 
  at MonoTouch.UIKit.UIControl.RemoveTarget (System.EventHandler notification, UIControlEvent events) [0x0004a] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIControl.cs:89 
  at MonoTouch.UIKit.UIControl.remove_TouchDown (System.EventHandler value) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIControl.cs:101 
  at Xactimate.iOS.UI.HomeScreen.LoadMoreView.CleanUpEvents () [0x00013] in /Users/i65781/sdGit/xactimate.ios/Xactimate.iOS/Xactimate.iOS/UI/HomeScreen/LoadMoreView.cs:84 
  at Xactimate.iOS.UI.HomeScreen.BaseLoadMoreProjectsTableViewController.CleanupLoadMoreView (Boolean disposing) [0x00013] in /Users/i65781/sdGit/xactimate.ios/Xactimate.iOS/Xactimate.iOS/UI/HomeScreen/BaseLoadMoreProjectsTableViewController.cs:170 
  at Xactimate.iOS.UI.HomeScreen.BaseLoadMoreProjectsTableViewController.Dispose (Boolean disposing) [0x00003] in /Users/i65781/sdGit/xactimate.ios/Xactimate.iOS/Xactimate.iOS/UI/HomeScreen/BaseLoadMoreProjectsTableViewController.cs:181 
  at Xactimate.iOS.UI.HomeScreen.CloudProjectsTableViewController.Dispose (Boolean disposing) [0x0003f] in /Users/i65781/sdGit/xactimate.ios/Xactimate.iOS/Xactimate.iOS/UI/HomeScreen/CloudProjectsTableViewController.cs:364 
  at MonoTouch.Foundation.NSObject.Dispose () [0x00000] in /Developer/MonoTouch/Source/maccore/src/Foundation/NSObject2.cs:119 
  at Xactimate.iOS.Logic.MemoryUtils.Dispose (IDisposable disposable) [0x00009] in /Users/i65781/sdGit/xactimate.ios/Xactimate.iOS/Xactimate.iOS.UILogic/MemoryUtils.cs:20 
  at Xactimate.iOS.UI.HomeScreen.CollapsableSplitViewController.Dispose (Boolean disposing) [0x00012] in /Users/i65781/sdGit/xactimate.ios/Xactimate.iOS/Xactimate.iOS/UI/HomeScreen/CollapsableSplitViewController.cs:251 
  at MonoTouch.Foundation.NSObject.Finalize () [0x00000] in /Developer/MonoTouch/Source/maccore/src/Foundation/NSObject2.cs:115
Comment 3 Bill 2014-06-30 14:14:43 UTC
Nevermind. Wrong stack trace.. wrong code that was built.