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
Downloader
class.Downloader
instance remains in memory after its use, potentially leading to increased memory usage and resource leakage.self
in asynchronous closures and Combine subscriptions.[weak self]
to capture self weakly in both thegetAllTasks
closure and the Combinesink
subscriber.This commit resolves the retain cycle issues, ensuring proper memory management for
Downloader
instances.Analysis
The
Downloader
class was found to have potential retain cycles due to strong references toself
within an async closure and a Combine sink subscription. These strong references prevent theDownloader
instance from being deallocated, leading to memory leaks.Problem Areas
Async Closure in URLSession
getAllTasks
:The closure passed to
urlSession?.getAllTasks
in the initializer referencedself
strongly, which could prevent the instance from being released if the closure retains it.Combine in
waitUntilDone
Method:The Combine
sink
subscriber inwaitUntilDone
held 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
self
weakly in both the async closure and the Combine.