Skip to content

Commit 9c0c715

Browse files
committed
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
1 parent 80683c2 commit 9c0c715

File tree

5 files changed

+35
-25
lines changed

5 files changed

+35
-25
lines changed

Examples/SDWebImage Demo.xcodeproj/project.pbxproj

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,8 @@
173173
53761299155AB74D005750A4 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
174174
5376129B155AB74D005750A4 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
175175
5376129D155AB74D005750A4 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
176-
537612A1155AB74D005750A4 /* SDWebImage Demo-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "SDWebImage Demo-Info.plist"; sourceTree = "<group>"; };
176+
537612A1155AB74D005750A4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
177177
537612A5155AB74D005750A4 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
178-
537612A7155AB74D005750A4 /* SDWebImage Demo-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SDWebImage Demo-Prefix.pch"; sourceTree = "<group>"; };
179178
537612A8155AB74D005750A4 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
180179
537612A9155AB74D005750A4 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
181180
537612AB155AB74D005750A4 /* MasterViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MasterViewController.h; sourceTree = "<group>"; };
@@ -393,9 +392,8 @@
393392
537612A0155AB74D005750A4 /* Supporting Files */ = {
394393
isa = PBXGroup;
395394
children = (
396-
537612A1155AB74D005750A4 /* SDWebImage Demo-Info.plist */,
395+
537612A1155AB74D005750A4 /* Info.plist */,
397396
537612A5155AB74D005750A4 /* main.m */,
398-
537612A7155AB74D005750A4 /* SDWebImage Demo-Prefix.pch */,
399397
);
400398
name = "Supporting Files";
401399
sourceTree = "<group>";
@@ -1031,8 +1029,7 @@
10311029
baseConfigurationReference = 789EDF4244C66933FDF3E181 /* Pods-SDWebImage iOS Demo.debug.xcconfig */;
10321030
buildSettings = {
10331031
GCC_PRECOMPILE_PREFIX_HEADER = YES;
1034-
GCC_PREFIX_HEADER = "SDWebImage Demo/SDWebImage Demo-Prefix.pch";
1035-
INFOPLIST_FILE = "SDWebImage Demo/SDWebImage Demo-Info.plist";
1032+
INFOPLIST_FILE = "SDWebImage Demo/Info.plist";
10361033
SDKROOT = iphoneos;
10371034
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
10381035
TARGETED_DEVICE_FAMILY = "1,2";
@@ -1044,8 +1041,7 @@
10441041
baseConfigurationReference = 0829378FA7103DD8649CCCF3 /* Pods-SDWebImage iOS Demo.release.xcconfig */;
10451042
buildSettings = {
10461043
GCC_PRECOMPILE_PREFIX_HEADER = YES;
1047-
GCC_PREFIX_HEADER = "SDWebImage Demo/SDWebImage Demo-Prefix.pch";
1048-
INFOPLIST_FILE = "SDWebImage Demo/SDWebImage Demo-Info.plist";
1044+
INFOPLIST_FILE = "SDWebImage Demo/Info.plist";
10491045
SDKROOT = iphoneos;
10501046
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
10511047
TARGETED_DEVICE_FAMILY = "1,2";
File renamed without changes.

Examples/SDWebImage Demo/SDWebImage Demo-Prefix.pch

Lines changed: 0 additions & 10 deletions
This file was deleted.

Podfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,20 @@ target 'Tests TV' do
6464
platform :tvos, '9.0'
6565
all_test_pods
6666
end
67+
68+
# Inject macro during SDWebImage Demo and Tests
69+
post_install do |installer_representation|
70+
installer_representation.generated_pod_targets.each do |target|
71+
if target.pod_name == "SDWebImage"
72+
build_settings = target.build_settings
73+
build_settings.each do |configuration, build_setting|
74+
if configuration == :debug
75+
config = build_setting.xcconfig
76+
old_value = config.attributes['GCC_PREPROCESSOR_DEFINITIONS']
77+
config.attributes['GCC_PREPROCESSOR_DEFINITIONS'] = old_value + ' SD_CHECK_CGIMAGE_RETAIN_SOURCE=1'
78+
config.save_as(target.xcconfig_path(configuration))
79+
end
80+
end
81+
end
82+
end
83+
end

SDWebImage/Core/SDImageIOAnimatedCoder.m

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
#import <ImageIO/ImageIO.h>
1919
#import <CoreServices/CoreServices.h>
2020

21+
#if SD_CHECK_CGIMAGE_RETAIN_SOURCE
22+
#import <dlfcn.h>
23+
24+
// SPI to check thread safe during Example and Test
25+
static CGImageSourceRef (*SDCGImageGetImageSource)(CGImageRef);
26+
#endif
27+
2128
// Specify File Size for lossy format encoding, like JPEG
2229
static NSString * kSDCGImageDestinationRequestedFileSize = @"kCGImageDestinationRequestedFileSize";
2330

24-
// Only assert on Debug mode
25-
#define SD_CHECK_CGIMAGE_RETAIN_SOURCE DEBUG && \
26-
((__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_15_0)) || \
27-
((__TV_OS_VERSION_MAX_ALLOWED >= __TVOS_15_0))
28-
2931
// This strip the un-wanted CGImageProperty, like the internal CGImageSourceRef in iOS 15+
3032
// However, CGImageCreateCopy still keep those CGImageProperty, not suit for our use case
3133
static CGImageRef __nullable SDCGImageCreateCopy(CGImageRef cg_nullable image) {
@@ -301,8 +303,13 @@ + (UIImage *)createFrameAtIndex:(NSUInteger)index source:(CGImageSourceRef)sourc
301303
if (@available(iOS 15, tvOS 15, *)) {
302304
// Assert here to check CGImageRef should not retain the CGImageSourceRef and has possible thread-safe issue (this is behavior on iOS 15+)
303305
// If assert hit, fire issue to https://github.com/SDWebImage/SDWebImage/issues and we update the condition for this behavior check
304-
extern CGImageSourceRef CGImageGetImageSource(CGImageRef);
305-
NSCAssert(!CGImageGetImageSource(imageRef), @"Animated Coder created CGImageRef should not retain CGImageSourceRef, which may cause thread-safe issue without lock");
306+
static dispatch_once_t onceToken;
307+
dispatch_once(&onceToken, ^{
308+
SDCGImageGetImageSource = dlsym(RTLD_DEFAULT, "CGImageGetImageSource");
309+
});
310+
if (SDCGImageGetImageSource) {
311+
NSCAssert(!SDCGImageGetImageSource(imageRef), @"Animated Coder created CGImageRef should not retain CGImageSourceRef, which may cause thread-safe issue without lock");
312+
}
306313
}
307314
#endif
308315

0 commit comments

Comments
 (0)