Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix retain cycles
Resolved retain cycles by capturing self weakly in async closure and Combine sink subscription. This prevents Downloader from being held in memory due to strong references in URLSession and Combine.
Report
Downloaderclass.Downloaderinstance remains in memory after its use, potentially leading to increased memory usage and resource leakage.selfin asynchronous closures and Combine subscriptions.[weak self]to capture self weakly in both thegetAllTasksclosure and the Combinesinksubscriber.This commit resolves the retain cycle issues, ensuring proper memory management for
Downloaderinstances.Analysis
The
Downloaderclass was found to have potential retain cycles due to strong references toselfwithin an async closure and a Combine sink subscription. These strong references prevent theDownloaderinstance from being deallocated, leading to memory leaks.Problem Areas
Async Closure in URLSession
getAllTasks:The closure passed to
urlSession?.getAllTasksin the initializer referencedselfstrongly, which could prevent the instance from being released if the closure retains it.Combine in
waitUntilDoneMethod:The Combine
sinksubscriber inwaitUntilDoneheld a strong reference toself, potentially causing a retain cycle as Combine retains its subscribers until they are explicitly canceled or deallocated.Solution
To prevent retain cycles, I've modified the code to capture
selfweakly in both the async closure and the Combine.