diff --git a/SDImageCache.m b/SDImageCache.m index 6a110ac7..1939036c 100644 --- a/SDImageCache.m +++ b/SDImageCache.m @@ -31,7 +31,7 @@ static SDImageCache *instance; // Init the disk cache NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); - diskCachePath = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"ImageCache"] retain]; + diskCachePath = SDWIReturnRetained([[paths objectAtIndex:0] stringByAppendingPathComponent:@"ImageCache"]); if (![[NSFileManager defaultManager] fileExistsAtPath:diskCachePath]) { @@ -78,13 +78,15 @@ static SDImageCache *instance; - (void)dealloc { - [memCache release], memCache = nil; - [diskCachePath release], diskCachePath = nil; - [cacheInQueue release], cacheInQueue = nil; + SDWIRelease(memCache); + SDWIRelease(diskCachePath); + SDWIRelease(cacheInQueue); [[NSNotificationCenter defaultCenter] removeObserver:self]; +#if ! __has_feature(objc_arc) [super dealloc]; +#endif } #pragma mark SDImageCache (class methods) @@ -128,7 +130,7 @@ static SDImageCache *instance; { // If no data representation given, convert the UIImage in JPEG and store it // This trick is more CPU/memory intensive and doesn't preserve alpha channel - UIImage *image = [[self imageFromKey:key fromDisk:YES] retain]; // be thread safe with no lock + UIImage *image = SDWIReturnRetained([self imageFromKey:key fromDisk:YES]); // be thread safe with no lock if (image) { #if TARGET_OS_IPHONE @@ -138,11 +140,11 @@ static SDImageCache *instance; NSData* jpegData = [NSBitmapImageRep representationOfImageRepsInArray: representations usingType: NSJPEGFileType properties:nil]; [fileManager createFileAtPath:[self cachePathForKey:key] contents:jpegData attributes:nil]; #endif - [image release]; + SDWIRelease(image); } } - [fileManager release]; + SDWIRelease(fileManager); } - (void)notifyDelegate:(NSDictionary *)arguments @@ -173,7 +175,7 @@ static SDImageCache *instance; - (void)queryDiskCacheOperation:(NSDictionary *)arguments { NSString *key = [arguments objectForKey:@"key"]; - NSMutableDictionary *mutableArguments = [[arguments mutableCopy] autorelease]; + NSMutableDictionary *mutableArguments = SDWIReturnAutoreleased([arguments mutableCopy]); UIImage *image = SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]]); @@ -215,9 +217,11 @@ static SDImageCache *instance; { keyWithData = [NSArray arrayWithObjects:key, nil]; } - [cacheInQueue addOperation:[[[NSInvocationOperation alloc] initWithTarget:self - selector:@selector(storeKeyWithDataToDisk:) - object:keyWithData] autorelease]]; + + NSInvocationOperation *operation = SDWIReturnAutoreleased([[NSInvocationOperation alloc] initWithTarget:self + selector:@selector(storeKeyWithDataToDisk:) + object:keyWithData]); + [cacheInQueue addOperation:operation]; } } @@ -293,7 +297,10 @@ static SDImageCache *instance; { [arguments setObject:info forKey:@"userInfo"]; } - [cacheOutQueue addOperation:[[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(queryDiskCacheOperation:) object:arguments] autorelease]]; + NSInvocationOperation *operation = SDWIReturnAutoreleased([[NSInvocationOperation alloc] initWithTarget:self + selector:@selector(queryDiskCacheOperation:) + object:arguments]); + [cacheOutQueue addOperation:operation]; } - (void)removeImageForKey:(NSString *)key diff --git a/SDWebImage.xcodeproj/project.pbxproj b/SDWebImage.xcodeproj/project.pbxproj index 2debffa4..0c3e9906 100644 --- a/SDWebImage.xcodeproj/project.pbxproj +++ b/SDWebImage.xcodeproj/project.pbxproj @@ -281,6 +281,8 @@ 53922D7D148C55820056699D /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + CLANG_ENABLE_OBJC_ARC = NO; + CLANG_WARN_OBJCPP_ARC_ABI = NO; DSTROOT = /tmp/SDWebImage.dst; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = ""; @@ -295,6 +297,8 @@ 53922D7E148C55820056699D /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + CLANG_ENABLE_OBJC_ARC = NO; + CLANG_WARN_OBJCPP_ARC_ABI = NO; DSTROOT = /tmp/SDWebImage.dst; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = ""; diff --git a/SDWebImageCompat.h b/SDWebImageCompat.h index 5c86b6b8..dfc24e23 100644 --- a/SDWebImageCompat.h +++ b/SDWebImageCompat.h @@ -21,6 +21,30 @@ #import #endif +#if ! __has_feature(objc_arc) +#define SDWIAutorelease(__v) ([__v autorelease]); +#define SDWIReturnAutoreleased SDWIAutorelease + +#define SDWIRetain(__v) ([__v retain]); +#define SDWIReturnRetained SDWIRetain + +#define SDWIRelease(__v) ([__v release], __v = nil); + +#define SDWIWeak +#else +// -fobjc-arc +#define SDWIAutorelease(__v) +#define SDWIReturnAutoreleased(__v) (__v) + +#define SDWIRetain(__v) +#define SDWIReturnRetained(__v) (__v) + +#define SDWIRelease(__v) + +#define SDWIWeak __unsafe_unretained +#endif + + NS_INLINE UIImage *SDScaledImageForPath(NSString *path, NSData *imageData) { if (!imageData) @@ -44,9 +68,9 @@ NS_INLINE UIImage *SDScaledImageForPath(NSString *path, NSData *imageData) } UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:UIImageOrientationUp]; - [image release]; + SDWIRelease(image) image = scaledImage; } - return [image autorelease]; + return SDWIReturnAutoreleased(image); } \ No newline at end of file diff --git a/SDWebImageDecoder.m b/SDWebImageDecoder.m index a9af798f..7361300d 100644 --- a/SDWebImageDecoder.m +++ b/SDWebImageDecoder.m @@ -22,7 +22,7 @@ static SDWebImageDecoder *sharedInstance; - (void)notifyDelegateOnMainThreadWithInfo:(NSDictionary *)dict { - [dict retain]; + SDWIRetain(dict); NSDictionary *decodeInfo = [dict objectForKey:DECODE_INFO_KEY]; UIImage *decodedImage = [dict objectForKey:DECOMPRESSED_IMAGE_KEY]; @@ -30,7 +30,7 @@ static SDWebImageDecoder *sharedInstance; NSDictionary *userInfo = [decodeInfo objectForKey:USER_INFO_KEY]; [delegate imageDecoder:self didFinishDecodingImage:decodedImage userInfo:userInfo]; - [dict release]; + SDWIRelease(dict); } - (void)decodeImageWithInfo:(NSDictionary *)decodeInfo @@ -71,13 +71,16 @@ static SDWebImageDecoder *sharedInstance; NSOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(decodeImageWithInfo:) object:decodeInfo]; [imageDecodingQueue addOperation:operation]; - [operation release]; + SDWIRelease(operation); } - (void)dealloc { - [imageDecodingQueue release], imageDecodingQueue = nil; + SDWIRelease(imageDecodingQueue); + +#if ! __has_feature(objc_arc) [super dealloc]; +#endif } + (SDWebImageDecoder *)sharedImageDecoder @@ -118,7 +121,7 @@ static SDWebImageDecoder *sharedInstance; UIImage *decompressedImage = [[UIImage alloc] initWithCGImage:decompressedImageRef]; CGImageRelease(decompressedImageRef); - return [decompressedImage autorelease]; + return SDWIReturnAutoreleased(decompressedImage); } @end diff --git a/SDWebImageDownloader.h b/SDWebImageDownloader.h index 4b4183c6..ffc6fa9d 100644 --- a/SDWebImageDownloader.h +++ b/SDWebImageDownloader.h @@ -17,7 +17,7 @@ extern NSString *const SDWebImageDownloadStopNotification; { @private NSURL *url; - id delegate; + SDWIWeak id delegate; NSURLConnection *connection; NSMutableData *imageData; id userInfo; diff --git a/SDWebImageDownloader.m b/SDWebImageDownloader.m index 12cacd17..e0956eb9 100644 --- a/SDWebImageDownloader.m +++ b/SDWebImageDownloader.m @@ -52,7 +52,7 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot name:SDWebImageDownloadStopNotification object:nil]; } - SDWebImageDownloader *downloader = [[[SDWebImageDownloader alloc] init] autorelease]; + SDWebImageDownloader *downloader = SDWIReturnAutoreleased([[SDWebImageDownloader alloc] init]); downloader.url = url; downloader.delegate = delegate; downloader.userInfo = userInfo; @@ -70,7 +70,7 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot { // In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15]; - self.connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease]; + self.connection = SDWIReturnAutoreleased([[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]); // If not in low priority mode, ensure we aren't blocked by UI manipulations (default runloop mode for NSURLConnection is NSEventTrackingRunLoopMode) if (!lowPriority) @@ -78,7 +78,7 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; } [connection start]; - [request release]; + SDWIRelease(request); if (connection) { @@ -132,7 +132,7 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot #else [delegate performSelector:@selector(imageDownloader:didFinishWithImage:) withObject:self withObject:image]; #endif - [image release]; + SDWIRelease(image) } } @@ -163,11 +163,14 @@ NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNot - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; - [url release], url = nil; - [connection release], connection = nil; - [imageData release], imageData = nil; - [userInfo release], userInfo = nil; + SDWIRelease(url); + SDWIRelease(connection); + SDWIRelease(imageData); + SDWIRelease(userInfo); + +#if ! __has_feature(objc_arc) [super dealloc]; +#endif } diff --git a/SDWebImageManager.m b/SDWebImageManager.m index fabf0f0e..fd20f154 100644 --- a/SDWebImageManager.m +++ b/SDWebImageManager.m @@ -30,13 +30,16 @@ static SDWebImageManager *instance; - (void)dealloc { - [downloadDelegates release], downloadDelegates = nil; - [downloaders release], downloaders = nil; - [cacheDelegates release], cacheDelegates = nil; - [cacheURLs release], cacheURLs = nil; - [downloaderForURL release], downloaderForURL = nil; - [failedURLs release], failedURLs = nil; + SDWIRelease(downloadDelegates); + SDWIRelease(downloaders); + SDWIRelease(cacheDelegates); + SDWIRelease(cacheURLs); + SDWIRelease(downloaderForURL); + SDWIRelease(failedURLs); + +#if ! __has_feature(objc_arc) [super dealloc]; +#endif } @@ -114,7 +117,7 @@ static SDWebImageManager *instance; while ((idx = [downloadDelegates indexOfObjectIdenticalTo:delegate]) != NSNotFound) { - SDWebImageDownloader *downloader = [[downloaders objectAtIndex:idx] retain]; + SDWebImageDownloader *downloader = SDWIReturnRetained([downloaders objectAtIndex:idx]); [downloadDelegates removeObjectAtIndex:idx]; [downloaders removeObjectAtIndex:idx]; @@ -126,7 +129,7 @@ static SDWebImageManager *instance; [downloaderForURL removeObjectForKey:downloader.url]; } - [downloader release]; + SDWIRelease(downloader); } } @@ -206,7 +209,7 @@ static SDWebImageManager *instance; - (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(UIImage *)image { - [downloader retain]; + SDWIRetain(downloader); SDWebImageOptions options = [[downloader.userInfo objectForKey:@"options"] intValue]; // Notify all the downloadDelegates with this downloader @@ -216,7 +219,9 @@ static SDWebImageManager *instance; SDWebImageDownloader *aDownloader = [downloaders objectAtIndex:uidx]; if (aDownloader == downloader) { - id delegate = [[[downloadDelegates objectAtIndex:uidx] retain] autorelease]; + id delegate = [downloadDelegates objectAtIndex:uidx]; + SDWIRetain(delegate); + SDWIAutorelease(delegate); if (image) { @@ -256,12 +261,12 @@ static SDWebImageManager *instance; // Release the downloader [downloaderForURL removeObjectForKey:downloader.url]; - [downloader release]; + SDWIRelease(downloader); } - (void)imageDownloader:(SDWebImageDownloader *)downloader didFailWithError:(NSError *)error; { - [downloader retain]; + SDWIRetain(downloader); // Notify all the downloadDelegates with this downloader for (NSInteger idx = (NSInteger)[downloaders count] - 1; idx >= 0; idx--) @@ -270,7 +275,9 @@ static SDWebImageManager *instance; SDWebImageDownloader *aDownloader = [downloaders objectAtIndex:uidx]; if (aDownloader == downloader) { - id delegate = [[[downloadDelegates objectAtIndex:uidx] retain] autorelease]; + id delegate = [downloadDelegates objectAtIndex:uidx]; + SDWIRetain(delegate); + SDWIAutorelease(delegate); if ([delegate respondsToSelector:@selector(webImageManager:didFailWithError:)]) { @@ -284,7 +291,7 @@ static SDWebImageManager *instance; // Release the downloader [downloaderForURL removeObjectForKey:downloader.url]; - [downloader release]; + SDWIRelease(downloader); } @end diff --git a/SDWebImagePrefetcher.m b/SDWebImagePrefetcher.m index 7dbf35c3..a06b6f07 100644 --- a/SDWebImagePrefetcher.m +++ b/SDWebImagePrefetcher.m @@ -106,7 +106,10 @@ static SDWebImagePrefetcher *instance; - (void)dealloc { self.prefetchURLs = nil; + +#if ! __has_feature(objc_arc) [super dealloc]; +#endif } @end