swalkner swalkner - 3 months ago 127
iOS Question

Xcode address sanitizer issue with `UIImage imageNamed:@""`

When activating address sanitizer in Xcode 8, I do get an issue at the following line of code:

UIImage *myImage = [UIImage imageNamed:imageName];


imageName
is an NSString, the according image exists in @1x, @2x,
@3x. The address sanitizer issue:


=================================================================


==2484==ERROR: AddressSanitizer: attempting free on address which was not malloc()-ed: 0x01d37310 in thread T0
#0 0x1fff73 in wrap_free (/private/var/mobile/Containers/Bundle/Application/C92A762E-DAAF-46D9-857B-151BAF97D0BB/Example.app/Frameworks/libclang_rt.asan_ios_dynamic.dylib+0x32f73)
#1 0x2b50fab5 in (/System/Library/PrivateFrameworks/CoreUI.framework/CoreUI+0x2ab5)
#2 0x2b50fbe5 in (/System/Library/PrivateFrameworks/CoreUI.framework/CoreUI+0x2be5)
#3 0x2b5463e1 in (/System/Library/PrivateFrameworks/CoreUI.framework/CoreUI+0x393e1)
#4 0x2b548307 in (/System/Library/PrivateFrameworks/CoreUI.framework/CoreUI+0x3b307)
#5 0x2b548233 in (/System/Library/PrivateFrameworks/CoreUI.framework/CoreUI+0x3b233)
#6 0x2b5606db in (/System/Library/PrivateFrameworks/CoreUI.framework/CoreUI+0x536db)
#7 0x2b560233 in (/System/Library/PrivateFrameworks/CoreUI.framework/CoreUI+0x53233)
#8 0x2924b611 in (/System/Library/Frameworks/UIKit.framework/UIKit+0x632611)
#9 0x2924b375 in (/System/Library/Frameworks/UIKit.framework/UIKit+0x632375)
#10 0x2924baf3 in (/System/Library/Frameworks/UIKit.framework/UIKit+0x632af3)
#11 0x28eaf8bd in (/System/Library/Frameworks/UIKit.framework/UIKit+0x2968bd)
#12 0x28d46e45 in (/System/Library/Frameworks/UIKit.framework/UIKit+0x12de45)
#13 0x76c4d1 in -[MyClass myMethod] (/private/var/mobile/Containers/Bundle/Application/C92A762E-DAAF-46D9-857B-151BAF97D0BB/Example.app/Frameworks/XY.framework/XY+0x2e4d1)
#16 0x28c2874b in (/System/Library/Frameworks/UIKit.framework/UIKit+0xf74b)
#17 0x28c284bb in (/System/Library/Frameworks/UIKit.framework/UIKit+0xf4bb)
#18 0x28c2e5e5 in (/System/Library/Frameworks/UIKit.framework/UIKit+0x155e5)
#19 0x28c2bf5d in (/System/Library/Frameworks/UIKit.framework/UIKit+0x12f5d)
#20 0x28c9707f in (/System/Library/Frameworks/UIKit.framework/UIKit+0x7e07f)
#21 0x28e8a505 in (/System/Library/Frameworks/UIKit.framework/UIKit+0x271505)
#22 0x28e8cacd in (/System/Library/Frameworks/UIKit.framework/UIKit+0x273acd)
#23 0x28e977a7 in (/System/Library/Frameworks/UIKit.framework/UIKit+0x27e7a7)
#24 0x28e8b285 in (/System/Library/Frameworks/UIKit.framework/UIKit+0x272285)
#25 0x2c09aebf in (/System/Library/PrivateFrameworks/FrontBoardServices.framework/FrontBoardServices+0x15ebf)
#26 0x2558e253 in (/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation+0xd0253)
#27 0x2558d517 in (/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation+0xcf517)
#28 0x2558c071 in (/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation+0xce071)
#29 0x254d799f in CFRunLoopRunSpecific (/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation+0x1999f)
#30 0x254d77b1 in CFRunLoopRunInMode (/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation+0x197b1)
#31 0x28c8da55 in (/System/Library/Frameworks/UIKit.framework/UIKit+0x74a55)
#32 0x28c88633 in UIApplicationMain (/System/Library/Frameworks/UIKit.framework/UIKit+0x6f633)
#34 0x34020aad in (/usr/lib/system/libdyld.dylib+0x1aad)
0x01d37310 is located 0 bytes inside of 34-byte region [0x01d37310,0x01d37332)


When I use a non-existing imageName, then the address sanitizer has no problem with that line.

The problem only seems to affect iOS 8, address sanitizer has no problems on iOS 9 and iOS 10 (latest beta). On iOS 8 it also works when I don't put the image in an asset but directly into the bundle.

Answer

This seems to be a bug in Xcode 8 Beta 6 (8S201h). When setting the Deployment Target of your App to iOS 8.3 calling UIImage imageNamed: will crash when the referenced image is coming from an asset catalog. Using UIImage imageNamed:inBundle:compatibleWithTraitCollection: will not make a difference.

Workarounds:

  • Set the deployment target < iOS 8.3. iOS 8.0/.1/.2 works for me, 8.3/.4 keeps crashing
  • put the image directly in the apps bundle, not an asset catalog

An example project for this issue can be found on GitHub