Linker vs Bindings and UI Thread Checks

The latest MonoTouch 5.3 (preview / alpha) introduced a new feature to help developers detect a common misuse of threads: using some UI elements on a background (not the main) thread.

What this means is that most of UIKit bindings now looks like this:

	[Export ("sizeToFit")]
	[CompilerGenerated]
	public virtual void SizeToFit ()
	{
		global::MonoTouch.UIKit.UIApplication.EnsureUIThread ();
		if (IsDirectBinding) {
			MonoTouch.ObjCRuntime.Messaging.void_objc_msgSend (this.Handle, selSizeToFit);
		} else {
			MonoTouch.ObjCRuntime.Messaging.void_objc_msgSendSuper (this.SuperHandle, selSizeToFit);
		}
	}

That’s very similar to earlier bindings except for a new line (#5) calling UIApplication.EnsureUIThread. This ensure the current thread is the application main thread, i.e. where UI updates are safe to do. If the code is not executing on the main thread then it will throw an UIKitThreadAccessException so you can find (and fix) such occurrence early in your application development.

By default this new check is kept (i.e. the linker won’t remove it) whenever debugging is enabled. For release builds the MonoTouch linker will remove the calls. The (de-compiled back to C#) code will then look just like it did before:

	[Export ("sizeToFit")]
	public virtual void SizeToFit ()
	{
		if (IsDirectBinding) {
			MonoTouch.ObjCRuntime.Messaging.void_objc_msgSend (this.Handle, selSizeToFit);
		} else {
			MonoTouch.ObjCRuntime.Messaging.void_objc_msgSendSuper (this.SuperHandle, selSizeToFit);
		}
	}

This is done because the call, like any, has a runtime cost and can be executed very frequently (e.g. inside a loop). You are also able to keep/remove this feature (independently of your debugging settings) by using new options in mtouch.

  • --force-thread-check will keep the check (e.g. in release builds, great for testing); and
  • --disable-thread-check will remove the check (e.g. even in debug builds, great if this becomes a performance nuisance).

You can also enable/disable this feature at runtime, as long as the linker has not removed it, by setting the UIApplication.CheckForIllegalCrossThreadCalls field to true or false.

Side Note: some readers might have noticed that the [CompilerGenerated] attribute (line #2 of the first listing) was also removed. This was not a mistake and it’s also not something new (to 5.3). This is simply one (of the many) custom attributes that the linker automagically removes from the final binaries to reduce their sizes.

Advertisements

About spouliot

Xamarin Hacker, Mono Contributor, Open Source Enthusiast with strong interests in Code Analysis, Cryptography and Security. Presently working on MonoTouch, previous projects were Moonlight and libgdiplus and a lot of the cryptographic work done on Mono.
This entry was posted in linker, mono, monotouch, xamarin. Bookmark the permalink.

4 Responses to Linker vs Bindings and UI Thread Checks

  1. Redth says:

    I love all these little tweaks you guys are incorporating. This UI Thread checking enhancement will be phenomenal for debugging, and catching all those times when weird things are happening and you can’t figure out why, only to realize you missed running something on the UI Thread. This feature is way more useful than you may initially think. Nice to know it’s being stripped out of the release builds too 🙂

    • spouliot says:

      Thanks! 🙂 This has been mentioned a few times in the past until Miguel implemented the feature in the generator, i.e. btouch. The linker on;y makes it safer, since the checks can be removed, and easier, than having different monotouch.dll, to use.

  2. Pingback: Linker vs Bindings and NewRefcount | surprisingly unimpressed

  3. Pingback: Linker vs Bindings and IsDirectBinding | take a bit, get a byte

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s