Skip to content
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

[SR-15378] Memory leaks with Data(contentsOf:) and String.replacingOccurrences(of:with:options:range:) #3193

Open
swift-ci opened this issue Oct 23, 2021 · 0 comments

Comments

@swift-ci
Copy link
Contributor

swift-ci commented Oct 23, 2021

Previous ID SR-15378
Radar None
Original Reporter Stefan Springer (JIRA User)
Type Bug
Environment

Swift 5.5. on macOS 11.6

Additional Detail from JIRA
Votes 0
Component/s Compiler, Foundation
Labels Bug
Assignee None
Priority Medium

md5: 75073759b3ea284a15da01390ca1c4b7

Issue Description:

The following examples need to be wrapped inside autoreleasepool / try autoreleasepool in order for the memory to be freed afterwards:

myString.replacingOccurrences(of: "[eo]", with: "l", options: .regularExpression, range: nil)
try Data(contentsOf: URL(fileURLWithPath: "myFile"))

This should not be necessary. And the effect of having memory leaks when not using autoreleasepool may come as a big surprise for beginners, since autoreleasepool is not even mentioned in the Swift book.

Furthermore, using autoreleasepool is not platform independent, and you then need to use something like the following for platform-independent code: https://github.com/apple/swift-docc/blob/38acdaddf610be6b943d82f0d9da628384784a81/Sources/SwiftDocC/Utility/FoundationExtensions/AutoreleasepoolShim.swift.

The issue seems to be a recurring topic, see e.g.:

Also see the discussion on https://forums.swift.org/t/the-role-of-autoreleasepool-in-swift-and-thoughts-about-memory-management-in-swift/52976.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant