GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #3776

New issue
Copy link
New issue
Copy link
Closed
Closed
Fix on macOS, SDAnimatedImagePlayer leaking SDDisplayLink instances and leaving CVDisplayLink threads running#3776
Copy link
Labels
leakMemory LeakmacOSAppKit for macOS
Milestone
5.20.1

Description

@renewedvision-kh
renewedvision-kh
opened on Dec 12, 2024
Issue body actions

New Issue Checklist

  • I have read and understood the CONTRIBUTING guide
  • I have read the Documentation
  • I have searched for a similar issue in the project and found none

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):
Screenshot 2024-12-12 at 4 30 29 PM

It is unclear if this is the correct solution to prevent the leak or if a better solution is available.

Activity

  1. dreampiggy commented on Dec 13, 2024

    @dreampiggy
    dreampiggy
    on Dec 13, 2024
    Contributor
    More actions

    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)

  2. dreampiggy
    added
    macOSAppKit for macOS
    leakMemory Leak
    on Dec 13, 2024
  3. dreampiggy commented on Dec 13, 2024

    @dreampiggy
    dreampiggy
    on Dec 13, 2024
    Contributor
    More actions

    @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
    }
    
  4. renewedvision-kh commented on Dec 13, 2024

    @renewedvision-kh
    renewedvision-kh
    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];
    }
    
  5. renewedvision-kh commented on Dec 13, 2024

    @renewedvision-kh
    renewedvision-kh
    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:

    Screenshot 2024-12-13 at 10 06 01 AM

    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.

  6. dreampiggy commented on Dec 16, 2024

    @dreampiggy
    dreampiggy
    on Dec 16, 2024
    Contributor
    More actions

    The SDDisplayLink is being held by an @autoreleasepool

    Sounds interesting, where this autorelasepool from (the old code, without your self-created autoreleasepool)

  7. dreampiggy commented on Dec 16, 2024

    @dreampiggy
    dreampiggy
    on Dec 16, 2024
    Contributor
    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];
    }
    

    This sounds a bug. When player stopped/dealloced. the DisplayPlayLink should also been stopped. I need to check the code why this failed

  8. renewedvision-kh commented on Jan 2, 2025

    @renewedvision-kh
    renewedvision-kh
    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.

  9. renewedvision-kh commented on Jan 2, 2025

    @renewedvision-kh
    renewedvision-kh
    on Jan 2, 2025
    ContributorAuthor
    More actions

    And FWIW this issue and this issue describes a similar problem with CVDisplayLinkOutputCallback not having an @autoreleasepool.

  10. dreampiggy commented on Jan 3, 2025

    @dreampiggy
    dreampiggy
    on Jan 3, 2025
    Contributor
    More actions

    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 ?

  11. renewedvision-kh commented on Jan 3, 2025

    @renewedvision-kh
    renewedvision-kh
    on Jan 3, 2025
    ContributorAuthor
    More actions

    @dreampiggy #3779

  12. renewedvision-kh commented on Jan 15, 2025

    @renewedvision-kh
    renewedvision-kh
    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?

  13. dreampiggy
    added this to the 5.20.1 milestone on Feb 17, 2025
  14. dreampiggy
    closed this as completedon Feb 20, 2025
  15. dreampiggy
    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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    leakMemory LeakmacOSAppKit for macOS

    Type

    No type

    Projects

    No projects

      Milestone

      • 5.20.1

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.