New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alamofire 4.0.0 memory leaks with uploadProgress and downloadProgress #1551
Comments
I've encountered a memory leak in AlamofireImage and found the leak is linked to the Request.validate() method. If I remove the validate() the memory leak disappears. |
The validate() leaks appear related to Validate.swift building closures while capturing strong self... e.g. lines 165, 241. These need changing to unowned self or refactored for weak self For example: Around Validation.swift Line 165 let validationExecution: () -> Void = {
if
let response = self.response,
self.delegate.error == nil,
case let .failure(error) = validation(self.request, response, self.delegate.data)
{
self.delegate.error = error
}
} Suggested change: let validationExecution: () -> Void = { [unowned self] in
if
let response = self.response,
self.delegate.error == nil,
case let .failure(error) = validation(self.request, response, self.delegate.data)
{
self.delegate.error = error
}
} There are several of these throughout the code that need to be handled. |
Previously, validation closures were executed on the Request’s operation queue and required `self` to be captured. Now that the `Request` holds the validation closure references in an internal array, it’s necessary to mark them as unowned.
Thank you everyone for all the detailed information. @asynchrony-ringo is certainly correct. The core issue stemmed from the validation closures not marking each internally stored closure as BackgroundTo give a bit of backstory, any closure that executes on the TaskDelegate's operation queue MUST capture a reference to
Once the task completes, the Alamofire 4In Alamofire 4, we moved all validation to be stored directly by the We'll look to get this fix deployed as soon as possible. Thanks again everyone for all your help here. Cheers. 🍻 |
* 'master' of github.com:Alamofire/Alamofire: (26 commits) [Issue Alamofire#1672] Added Request retryCount property to support RequestRetrier. Fixed test target compiler warning by not requiring app extension APIs only. [PR Alamofire#1670] Fixed compiler issue with DownloadRequest in AF4 migration guide. SPM package file now excludes tests since current configuration is not supported. Pulled in the release notes for the 3.5.1 release into the CHANGELOG. Added docstrings and note to README about resumeData and background sessions. Add 10.12 check for tests results, as macOS results are now correct. Added missing dashes in CHANGELOG…no functional changes. [PR Alamofire#1633] Fixed sample code in the README where error was used incorrectly. [PR Alamofire#1625] Fixed Session Manager internal links in the README. [PR Alamofire#1615] Fixed compiler error in response validation sample code in README. Added release notes to the CHANGELOG and bumped the version to 4.0.1. Removed excess whitespace from some of the docstrings…no functional changes. Removed framework and test target overrides that duplicated project settings. [PR Alamofire#1612] Fixed compilation issue in response handler section of the README. Fixed up if statement formatting…no functional changes. Added test verifying download request can be resumed with resume data. [Issue Alamofire#1551] Fixed retain cycle when using Validation clsoures. Fixed alignment in Travis yaml file…no functional changes. [PR Alamofire#1520] Renamed OSX to macOS throughout the project. ...
* 'master' of github.com:Alamofire/Alamofire: (27 commits) [PR Alamofire#1721] Improve embedded framework installation instructions. [PR Alamofire#1722] Update README for proper error checking in download. [Issue Alamofire#1672] Added Request retryCount property to support RequestRetrier. Fixed test target compiler warning by not requiring app extension APIs only. [PR Alamofire#1670] Fixed compiler issue with DownloadRequest in AF4 migration guide. SPM package file now excludes tests since current configuration is not supported. Pulled in the release notes for the 3.5.1 release into the CHANGELOG. Added docstrings and note to README about resumeData and background sessions. Add 10.12 check for tests results, as macOS results are now correct. Added missing dashes in CHANGELOG…no functional changes. [PR Alamofire#1633] Fixed sample code in the README where error was used incorrectly. [PR Alamofire#1625] Fixed Session Manager internal links in the README. [PR Alamofire#1615] Fixed compiler error in response validation sample code in README. Added release notes to the CHANGELOG and bumped the version to 4.0.1. Removed excess whitespace from some of the docstrings…no functional changes. Removed framework and test target overrides that duplicated project settings. [PR Alamofire#1612] Fixed compilation issue in response handler section of the README. Fixed up if statement formatting…no functional changes. Added test verifying download request can be resumed with resume data. [Issue Alamofire#1551] Fixed retain cycle when using Validation clsoures. ...
The problem still exists. I am getting leaks If I validate the requests. The leak is gone If I remove the validation part. |
Hi,
I think that I have identified a memory leak when I am using uploadProgress or downloadProgress in my code.
Here is the code example that is generating memory leak.
If I don't use uploadProgress I don't have memory leaks and all my objects are correctly deinit. But I want to know the progression of an upload.
I find a way to avoid this problem in Alamofire. In UploadRequest I have replaced uploadProgress with this one:
After that, memory leaks disappeared. I think the same thing could be done with downloadProgress.
I want to open issue on this because I am sure that my solution is not the the good one.
The text was updated successfully, but these errors were encountered: