From e7210f104df6a8a3e2ac8e724f1b8c1f4088d55f Mon Sep 17 00:00:00 2001 From: "snow.wu" Date: Fri, 20 Aug 2021 19:43:04 +0800 Subject: [PATCH] Fix SDWebImageCombinedOperation was leak. When the task is executed in a concurrent queue, when the user scrolls up and down in the List, the function call "sd_setImageWithURL:placeholderImage:options:progress:completed:" will be triggered to "SDWebImageCombinedOperation cancel", and the real task will be Cancel was called, which caused the task to be executed in multi-threaded execution until the "operation.isCancelled" in "SDImageCache queryDiskCacheForKey:done:" was returned. Through the principle of opening and closing, when we add, we guarantee that there must be a place to be removed. Since the block supports the ability of external coverage, the approach here is the same as operation.cancelBlock = ^{ [subOperation cancel]; This code does not conflict There is a bug that SDWebImageCombinedOperation is not released. --- SDWebImage/SDWebImageManager.m | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index d331a4a5..a7cda748 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -148,6 +148,30 @@ @synchronized (self.runningOperations) { [self.runningOperations addObject:operation]; + + /* + Fix: Memory bug + When the task is executed in a concurrent queue, when the user scrolls up and down in the List, the function call "sd_setImageWithURL:placeholderImage:options:progress:completed:" will be triggered to "SDWebImageCombinedOperation cancel", + and the real task will be Cancel was called, which caused the task to be executed in multi-threaded execution until the "operation.isCancelled" in "SDImageCache queryDiskCacheForKey:done:" was returned. + + Through the principle of opening and closing, when we add, we guarantee that there must be a place to be removed. + + Since the block supports the ability of external coverage, the approach here is the same as + operation.cancelBlock = ^{ + [subOperation cancel]; + + This code does not conflict + + There is a bug that SDWebImageCombinedOperation is not released. + */ + __weak __typeof(self) weakSelf = self; + operation.cancelBlock = ^{ + __strong __typeof(weakSelf) strongSelf = weakSelf; + __strong __typeof(weakOperation) strongOperation = weakOperation; + if (strongOperation) { + [strongSelf.runningOperations removeObject:strongOperation]; + } + }; } NSString *key = [self cacheKeyForURL:url];