GeezerVison is showing github.com — Fix on macOS, SDAnimatedImagePlayer leaking SDDisplayLink instances and leaving CVDisplayLink threads running · Issue #3776 · SDWebImage/SDWebImage
Fix on macOS, SDAnimatedImagePlayer leaking SDDisplayLink instances and leaving CVDisplayLink threads running #3776DescriptionNew Issue Checklist
Issue Info
| Info |
Value |
| Platform Name |
macos |
| Platform Version |
15.1.1 |
| SDWebImage Version |
5.20.0 |
| Integration Method |
SPM |
| Xcode Version |
16.2 |
| Repro rate |
100% |
| Repro with our demo prj |
n/a |
| Demo project link |
n/a |
Issue Description and Steps
When an SDAnimatedImagePlayer creates a SDDisplayLink in -[SDAnimatedImagePlayer displayLink] the SDDisplayLink is not released when the SDAnimatedImagePlayer is deallocated. This results in a proliferation of CVDisplayLink threads.
The current implementation of '-[SDAnimatedImagePlayer dealloc]' is:
- (void)dealloc {
// Dereference the frame pool, when zero the frame pool for provider will dealloc
[SDImageFramePool unregisterProvider:self.animatedProvider];
}
And I se nothing that would suggest that the SDDisplayLink is stopped or removed from the RunLoop. When I change the code to something link:
- (void)dealloc {
// Dereference the frame pool, when zero the frame pool for provider will dealloc
[SDImageFramePool unregisterProvider:self.animatedProvider];
[_displayLink stop];
[_displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:_runLoopMode];
}
Then I no longer see a proliferation of CVDisplayLink threads (as seen in Xcode):

It is unclear if this is the correct solution to prevent the leak or if a better solution is available. Activitydreampiggy commented on Dec 13, 2024 MacOS only issue ?
Only CVDisplayLinkRef has this issue ? Because this implementation use 3 different solutions on 3 different platforms.
on iOS this implementation use CADisplayLink, which is a NSObject and use ARC to references, will been deallocated automatically (on the dealloc method, SDAnimatedImagePlayer will remove references to SDWeakProxy, which SDWeakProxy references to CADisplayLink, they will all dealloc) dreampiggy commented on Dec 13, 2024 @renewedvision-kh
Updated: Do you means, that SDDisplayLink's dealloc method does not get called on macOS, even when that SDAnimatedImagePlayer dealloc called?
Can you set a breakpoint here ?
@implementation SDDisplayLink
- (void)dealloc {
#if SD_MAC
if (_displayLink) {
CVDisplayLinkStop(_displayLink);
CVDisplayLinkRelease(_displayLink);
_displayLink = NULL;
}
#endif
}
renewedvision-kh commented on Dec 13, 2024 ContributorAuthor More actions @dreampiggy I am not seeing -[SDDisplayLink dealloc] getting called.
FWIW I did some more testing and if I add a call to -[SDDisplayLink stop] from -[SDAnimatedImagePlayer dealloc] then I do see -[SDDisplayLink dealloc] getting called. Specifically this change seems to address the issue for me:
- (void)dealloc {
// Dereference the frame pool, when zero the frame pool for provider will dealloc
[SDImageFramePool unregisterProvider:self.animatedProvider];
[_displayLink stop];
}
renewedvision-kh commented on Dec 13, 2024 ContributorAuthor More actions @dreampiggy OK, I did a little more digging and this is what I see. The SDDisplayLink is being held by an @autoreleasepool:
Here the left most object is the thread, then the @autoreleasepool, then the SDDisplayLink and the right most object is the CVDisplayLink. When I wrap the callback code in its own @autoreleasepool like so:
#if SD_MAC
static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *inNow, const CVTimeStamp *inOutputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, void *displayLinkContext) {
@autoreleasepool {
// CVDisplayLink callback is not on main queue
// Actually `SDWeakProxy` but not `SDDisplayLink`
SDDisplayLink *object = (__bridge SDDisplayLink *)displayLinkContext;
if (!object) return kCVReturnSuccess;
// CVDisplayLink does not use runloop, but we can provide similar behavior for modes
// May use `default` runloop to avoid extra callback when in `eventTracking` (mouse drag, scroll) or `modalPanel` (modal panel)
NSString *runloopMode = object.runloopMode;
if (![runloopMode isEqualToString:NSRunLoopCommonModes] && ![runloopMode isEqualToString:NSRunLoop.mainRunLoop.currentMode]) {
return kCVReturnSuccess;
}
CVTimeStamp outputTime = inOutputTime ? *inOutputTime : *inNow;
// `SDWeakProxy` is weak, so it's safe to dispatch to main queue without leak
dispatch_async(dispatch_get_main_queue(), ^{
object.outputTime = outputTime;
[object displayLinkDidRefresh:(__bridge id)(displayLink)];
});
return kCVReturnSuccess;
}
}
#endif
Then I see -[SDDisplayLink dealloc] called as expected. dreampiggy commented on Dec 16, 2024
The SDDisplayLink is being held by an @autoreleasepool
Sounds interesting, where this autorelasepool from (the old code, without your self-created autoreleasepool) dreampiggy commented on Dec 16, 2024
@dreampiggy I am not seeing -[SDDisplayLink dealloc] getting called.
FWIW I did some more testing and if I add a call to -[SDDisplayLink stop] from -[SDAnimatedImagePlayer dealloc] then I do see -[SDDisplayLink dealloc] getting called. Specifically this change seems to address the issue for me:
- (void)dealloc {
// Dereference the frame pool, when zero the frame pool for provider will dealloc
[SDImageFramePool unregisterProvider:self.animatedProvider];
[_displayLink stop];
}
This sounds a bug. When player stopped/dealloced. the DisplayPlayLink should also been stopped. I need to check the code why this failed renewedvision-kh commented on Jan 2, 2025 ContributorAuthor More actions @dreampiggy Personally, adding an @autoreleasepool to DisplayLinkCallback seems like a more correct fix as this will also allow any other objects added to the auto release pool to be cleaned up each time the CVDisplayLink calls the function. In other words my stab in the dark calling [_displayLink stop] isn't the ideal fix. renewedvision-kh commented on Jan 2, 2025 ContributorAuthor More actions And FWIW this issue and this issue describes a similar problem with CVDisplayLinkOutputCallback not having an @autoreleasepool. dreampiggy commented on Jan 3, 2025 Sorry for delay. I'll have a check later and maybe a fix release provided.
Or, can you just submit this PR for your description ? Seems a simple custom autoreleasepool will fix this on macOS ? renewedvision-kh commented on Jan 3, 2025 ContributorAuthor More actions renewedvision-kh commented on Jan 15, 2025 ContributorAuthor More actions @dreampiggy Can #3779 be merged? The "Cocoapods Demo" and "Unit Test (iOS)" hav nothing to do with the changes made in the PR. Also, is there an approximate target data for the next public release of SDWebImage? changed the title [-]SDAnimatedImagePlayer leaking SDDisplayLink instances and leaving CVDisplayLink threads running[/-] [+]Fix on macOS, SDAnimatedImagePlayer leaking SDDisplayLink instances and leaving CVDisplayLink threads running[/+] on Feb 20, 2025
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|