Bug 40520 - UIImage.FromBundle incorrectly checks for UIApplication.CheckForIllegalCrossThreadCalls in iOS 9+
Summary: UIImage.FromBundle incorrectly checks for UIApplication.CheckForIllegalCrossT...
Status: CONFIRMED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll (show other bugs)
Version: XI 9.6 (iOS 9.3)
Hardware: PC Windows
: Low enhancement
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-04-19 13:55 UTC by Jerome Laban
Modified: 2016-04-19 15:02 UTC (History)
2 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

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:

#if DEBUG
   UIImage FromBundleSafe (NSBundle b)
   {
       UIApplication.CheckForIllegalCrossThreadCalls = false;
       var i = UIImage.FromBundle (b);
       UIApplication.CheckForIllegalCrossThreadCalls = true;
       return i;
   }
#else
   UIImage FromBundleSafe (NSBundle b)
   {
       return UIImage.FromBundle (b);
   }
#endif
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).

Note You need to log in before you can comment on or make changes to this bug.