Bug 40520 - UIImage.FromBundle incorrectly checks for UIApplication.CheckForIllegalCrossThreadCalls in iOS 9+
Summary: UIImage.FromBundle incorrectly checks for UIApplication.CheckForIllegalCrossT...
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.6 (iOS 9.3)
Hardware: PC Windows
: Low enhancement
Target Milestone: 15.7
Assignee: Sebastien Pouliot
Depends on:
Reported: 2016-04-19 13:55 UTC by Jerome Laban
Modified: 2018-03-08 21:04 UTC (History)
3 users (show)

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:

Description Jerome Laban 2016-04-19 13:55:26 UTC
The Xamarin.iOS binding for UIImage.FromBundle incorrectly enforces UI Thread access for iOS 9.0 and later.

See for details: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/index.html#//apple_ref/occ/clm/UIImage/imageNamed:

"In iOS 9 and later, this method is thread safe."
Comment 1 Sebastien Pouliot 2016-04-19 14:15:13 UTC
Thanks for the information!

The threading checks are debug helpers, i.e. they are removed from release builds (by the managed linker), unless extra arguments are given to mtouch, as they have a performance impact on applications (e.g. when used in tight loops).

Right now there's no way to flag specific versions (changes are uncommon) and removing the check would cause issues to anyone who wants to support older versions of iOS (and the product still supports iOS 6.x).

If needed you can workaround this by disabling the check before the UIImage.FromBundle call (and re-enabling it afterward). E.g. something like:

   UIImage FromBundleSafe (NSBundle b)
       UIApplication.CheckForIllegalCrossThreadCalls = false;
       var i = UIImage.FromBundle (b);
       UIApplication.CheckForIllegalCrossThreadCalls = true;
       return i;
   UIImage FromBundleSafe (NSBundle b)
       return UIImage.FromBundle (b);
Comment 2 Jerome Laban 2016-04-19 14:48:02 UTC
Thanks Sebastien! Knowing this check is removed by the linker helps quite a bit indeed.

Also, changing the value CheckForIllegalCrossThreadCalls is thread unsafe, making a window of vulnerability that may set CheckForIllegalCrossThreadCalls back at an inopportune time.
Comment 3 Sebastien Pouliot 2016-04-19 15:02:19 UTC
Right, _something like_ the above, e.g. the method signature does not match the API either, any exception thrown won't set the field back to true...

Your implementation can either add locking or call the underlying objc_msgSend yourself, e.g. copy/paste from assembly browser and remove the UIApplication.EnsureUIThread call.

The important bit is to have your release build "free" of any hack (e.g. adding a version check) so the rest of the tooling can continue to do it's job (te.g. he linker only removes the check on code that was generated, not user code).
Comment 4 Sebastien Pouliot 2018-02-08 03:20:58 UTC
This was made [ThreadSafe] a while ago [1] but before closing it I'm adding an `[Advice]` so the IDE will show a message to say it was not always (before iOS9) this way.


[1] maccore/e39d21e26dd863d019269117d4c110c3ac8ce9f5