From 9c0c715d6aba8bfaec5f5440c4e2cd581b321bae Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 14 Nov 2022 17:07:29 +0800 Subject: [PATCH] Move the SD_CHECK_CGIMAGE_RETAIN_SOURCE assertion to SDWebImage Demo and Tests only This does not effects user who use this framework, only useful for ourselves to ensure Apple does not break our code --- .../SDWebImage Demo.xcodeproj/project.pbxproj | 12 ++++------- ...{SDWebImage Demo-Info.plist => Info.plist} | 0 .../SDWebImage Demo-Prefix.pch | 10 --------- Podfile | 17 +++++++++++++++ SDWebImage/Core/SDImageIOAnimatedCoder.m | 21 ++++++++++++------- 5 files changed, 35 insertions(+), 25 deletions(-) rename Examples/SDWebImage Demo/{SDWebImage Demo-Info.plist => Info.plist} (100%) delete mode 100644 Examples/SDWebImage Demo/SDWebImage Demo-Prefix.pch diff --git a/Examples/SDWebImage Demo.xcodeproj/project.pbxproj b/Examples/SDWebImage Demo.xcodeproj/project.pbxproj index 8def2879..ddf8a658 100644 --- a/Examples/SDWebImage Demo.xcodeproj/project.pbxproj +++ b/Examples/SDWebImage Demo.xcodeproj/project.pbxproj @@ -173,9 +173,8 @@ 53761299155AB74D005750A4 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; 5376129B155AB74D005750A4 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 5376129D155AB74D005750A4 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; - 537612A1155AB74D005750A4 /* SDWebImage Demo-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "SDWebImage Demo-Info.plist"; sourceTree = ""; }; + 537612A1155AB74D005750A4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 537612A5155AB74D005750A4 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; - 537612A7155AB74D005750A4 /* SDWebImage Demo-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SDWebImage Demo-Prefix.pch"; sourceTree = ""; }; 537612A8155AB74D005750A4 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 537612A9155AB74D005750A4 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 537612AB155AB74D005750A4 /* MasterViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MasterViewController.h; sourceTree = ""; }; @@ -393,9 +392,8 @@ 537612A0155AB74D005750A4 /* Supporting Files */ = { isa = PBXGroup; children = ( - 537612A1155AB74D005750A4 /* SDWebImage Demo-Info.plist */, + 537612A1155AB74D005750A4 /* Info.plist */, 537612A5155AB74D005750A4 /* main.m */, - 537612A7155AB74D005750A4 /* SDWebImage Demo-Prefix.pch */, ); name = "Supporting Files"; sourceTree = ""; @@ -1031,8 +1029,7 @@ baseConfigurationReference = 789EDF4244C66933FDF3E181 /* Pods-SDWebImage iOS Demo.debug.xcconfig */; buildSettings = { GCC_PRECOMPILE_PREFIX_HEADER = YES; - GCC_PREFIX_HEADER = "SDWebImage Demo/SDWebImage Demo-Prefix.pch"; - INFOPLIST_FILE = "SDWebImage Demo/SDWebImage Demo-Info.plist"; + INFOPLIST_FILE = "SDWebImage Demo/Info.plist"; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = "iphonesimulator iphoneos"; TARGETED_DEVICE_FAMILY = "1,2"; @@ -1044,8 +1041,7 @@ baseConfigurationReference = 0829378FA7103DD8649CCCF3 /* Pods-SDWebImage iOS Demo.release.xcconfig */; buildSettings = { GCC_PRECOMPILE_PREFIX_HEADER = YES; - GCC_PREFIX_HEADER = "SDWebImage Demo/SDWebImage Demo-Prefix.pch"; - INFOPLIST_FILE = "SDWebImage Demo/SDWebImage Demo-Info.plist"; + INFOPLIST_FILE = "SDWebImage Demo/Info.plist"; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = "iphonesimulator iphoneos"; TARGETED_DEVICE_FAMILY = "1,2"; diff --git a/Examples/SDWebImage Demo/SDWebImage Demo-Info.plist b/Examples/SDWebImage Demo/Info.plist similarity index 100% rename from Examples/SDWebImage Demo/SDWebImage Demo-Info.plist rename to Examples/SDWebImage Demo/Info.plist diff --git a/Examples/SDWebImage Demo/SDWebImage Demo-Prefix.pch b/Examples/SDWebImage Demo/SDWebImage Demo-Prefix.pch deleted file mode 100644 index 36e5c4de..00000000 --- a/Examples/SDWebImage Demo/SDWebImage Demo-Prefix.pch +++ /dev/null @@ -1,10 +0,0 @@ -// -// Prefix header for all source files of the 'SDWebImage Demo' target in the 'SDWebImage Demo' project -// - -#import - -#ifdef __OBJC__ - #import - #import -#endif diff --git a/Podfile b/Podfile index e58e7b39..264b2698 100644 --- a/Podfile +++ b/Podfile @@ -64,3 +64,20 @@ target 'Tests TV' do platform :tvos, '9.0' all_test_pods end + +# Inject macro during SDWebImage Demo and Tests +post_install do |installer_representation| + installer_representation.generated_pod_targets.each do |target| + if target.pod_name == "SDWebImage" + build_settings = target.build_settings + build_settings.each do |configuration, build_setting| + if configuration == :debug + config = build_setting.xcconfig + old_value = config.attributes['GCC_PREPROCESSOR_DEFINITIONS'] + config.attributes['GCC_PREPROCESSOR_DEFINITIONS'] = old_value + ' SD_CHECK_CGIMAGE_RETAIN_SOURCE=1' + config.save_as(target.xcconfig_path(configuration)) + end + end + end + end +end diff --git a/SDWebImage/Core/SDImageIOAnimatedCoder.m b/SDWebImage/Core/SDImageIOAnimatedCoder.m index 56fbf22c..04aff872 100644 --- a/SDWebImage/Core/SDImageIOAnimatedCoder.m +++ b/SDWebImage/Core/SDImageIOAnimatedCoder.m @@ -18,14 +18,16 @@ #import #import +#if SD_CHECK_CGIMAGE_RETAIN_SOURCE +#import + +// SPI to check thread safe during Example and Test +static CGImageSourceRef (*SDCGImageGetImageSource)(CGImageRef); +#endif + // Specify File Size for lossy format encoding, like JPEG static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestinationRequestedFileSize"; -// Only assert on Debug mode -#define SD_CHECK_CGIMAGE_RETAIN_SOURCE DEBUG && \ - ((__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_15_0)) || \ - ((__TV_OS_VERSION_MAX_ALLOWED >= __TVOS_15_0)) - // This strip the un-wanted CGImageProperty, like the internal CGImageSourceRef in iOS 15+ // However, CGImageCreateCopy still keep those CGImageProperty, not suit for our use case static CGImageRef __nullable SDCGImageCreateCopy(CGImageRef cg_nullable image) { @@ -301,8 +303,13 @@ static CGImageRef __nullable SDCGImageCreateCopy(CGImageRef cg_nullable image) { if (@available(iOS 15, tvOS 15, *)) { // Assert here to check CGImageRef should not retain the CGImageSourceRef and has possible thread-safe issue (this is behavior on iOS 15+) // If assert hit, fire issue to https://github.com/SDWebImage/SDWebImage/issues and we update the condition for this behavior check - extern CGImageSourceRef CGImageGetImageSource(CGImageRef); - NSCAssert(!CGImageGetImageSource(imageRef), @"Animated Coder created CGImageRef should not retain CGImageSourceRef, which may cause thread-safe issue without lock"); + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + SDCGImageGetImageSource = dlsym(RTLD_DEFAULT, "CGImageGetImageSource"); + }); + if (SDCGImageGetImageSource) { + NSCAssert(!SDCGImageGetImageSource(imageRef), @"Animated Coder created CGImageRef should not retain CGImageSourceRef, which may cause thread-safe issue without lock"); + } } #endif