Xcode 解决 `UIImage imageNamed:@""` 的消毒问题

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

在 Xcode 8 中激活地址清理程序时,我在以下代码行遇到问题:

UIImage *myImage = [UIImage imageNamed:imageName];

imageName是一个NSString,对应的图片存在于@1x,@2x, @3x。地址清理程序问题:

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

==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)

当我使用不存在的 imageName 时,地址清理程序对该行没有问题。

这个问题似乎只影响 iOS 8,address sanitizer 在 iOS 9 和 iOS 10(最新测试版)上没有问题。在 iOS 8 上,当我不将图像放入资产中而是直接放入包中时,它也有效。

这似乎是 Xcode 8 Beta 6 (8S201h) 中的错误。 当将应用程序的部署目标设置为 iOS 8.3 时,如果引用的图像来自资产目录,调用 UIImage imageNamed: 将会崩溃。使用 UIImage imageNamed:inBundle:compatibleWithTraitCollection: 不会有什么不同。

解决方法:

  • 设置部署目标 < iOS 8.3。 iOS 8.0/.1/.2 适合我,8.3/.4 老是崩溃
  • 将图像直接放在应用程序包中,而不是资产目录中

可以在 GitHub

上找到此问题的示例项目