SDWebImage: [SDMemoryCache removeAllObjects] crash

New Issue Checklist

Issue Info

Info Value
Platform Name iOS
Platform Version 11.4, 11.4.1, 12.0.0, 12.0.1
SDWebImage Version e.g. 4.2.0 / 4.1.0
Integration Method cocoapods
Xcode Version Xcode 10.0
Repro rate sometimes
Repro with our demo prj
Demo project link

Issue Description and Steps

I’m getting following error while application tries to clean memory cache after entering background state. This crash started to appear since SDWebImage 4.4.2, but doesn’t happen every time.

-(void) applicationDidEnterBackground:(UIApplication *)application {
    [SDWebImageManager.sharedManager.imageCache clearMemory];
}

Here is crash stacktrace:

Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x182bfc910 objc_msgSend + 16
1  Foundation                     0x18444f6b8 empty + 72
2  Foundation                     0x1843a1db8 -[NSConcreteMapTable removeAllItems] + 64
3  MyApp                          0x1005ce384 -[SDMemoryCache removeAllObjects] (SDImageCache.m:130)
4  MyApp                          0x1005d12c4 -[SDImageCache clearMemory] (SDImageCache.m:619)
5  MyApp                          0x1004de84c -[AppDelegate applicationDidEnterBackground:] (AppDelegate.m:269)
6  UIKit                          0x18da21984 __47-[UIApplication _applicationDidEnterBackground]_block_invoke + 184
7  UIKit                          0x18d6a89a0 +[UIViewController _performWithoutDeferringTransitions:] + 112
8  UIKit                          0x18da2185c -[UIApplication _applicationDidEnterBackground] + 96
9  UIKit                          0x18dc3b17c -[__UICanvasLifecycleMonitor_Compatability deactivateEventsOnly:withContext:forceExit:completion:] + 1000
10 UIKit                          0x18e29e89c __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 376
11 UIKit                          0x18d6081ec -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 432
12 UIKit                          0x18e083ac8 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 220
13 UIKit                          0x18e1d1bf8 _performActionsWithDelayForTransitionContext + 112
14 UIKit                          0x18d607c0c -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 248
15 UIKit                          0x18d6075a8 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 368
16 UIKit                          0x18d645334 -[UIApplicationSceneClientAgent scene:handleEvent:withCompletion:] + 468
17 FrontBoardServices             0x186230f24 __80-[FBSSceneImpl updater:didUpdateSettings:withDiff:transitionContext:completion:]_block_invoke.362 + 212
18 libdispatch.dylib              0x183320a60 _dispatch_client_callout + 16
19 libdispatch.dylib              0x183328170 _dispatch_block_invoke_direct$VARIANT$mp + 224
20 FrontBoardServices             0x186264878 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 36
21 FrontBoardServices             0x18626451c -[FBSSerialQueue _performNext] + 404
22 FrontBoardServices             0x186264ab8 -[FBSSerialQueue _performNextFromRunLoopSource] + 56
23 CoreFoundation                 0x1839d7404 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
24 CoreFoundation                 0x1839d6c2c __CFRunLoopDoSources0 + 276
25 CoreFoundation                 0x1839d479c __CFRunLoopRun + 1204
26 CoreFoundation                 0x1838f4da8 CFRunLoopRunSpecific + 552
27 GraphicsServices               0x1858da020 GSEventRunModal + 100
28 UIKit                          0x18d914758 UIApplicationMain + 236
29 MyApp                          0x1004f6c3c main (main.m:14)
30 libdyld.dylib                  0x183385fc0 start + 4

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 23 (16 by maintainers)

Most upvoted comments

@dreampiggy @bpoplauschi I want to try to fix it like below, what’s your guys opinion? 🤔

[self.weakCache setObject:obj forKey:[[key mutableCopy] copy]];

@dreampiggy Issue was reported on devices running iOS 10 up to iOS 12, both iPhone and iPad. There were no jailbroken devices reported.

If the key is TaggedPointer, the key will not follow the strong (retain + 1) semantic behavior, According to https://opensource.apple.com/source/objc4/objc4-723/runtime/NSObject.mm.auto.html

objc_retain(id obj)
{
    if (!obj) return obj;
    if (obj->isTaggedPointer()) return obj;
    return obj->retain();
}