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-185] Memory leak in NSData.dataUsingEncoding() #4377

Closed
swift-ci opened this issue Dec 10, 2015 · 3 comments
Closed

[SR-185] Memory leak in NSData.dataUsingEncoding() #4377

swift-ci opened this issue Dec 10, 2015 · 3 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-185
Radar None
Original Reporter nickygerritsen (JIRA User)
Type Bug
Status Closed
Resolution Invalid
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: f97dfbe68c09ac47b338328c0eb2f062

Issue Description:

It seems NSData.dataUsingEncoding(_) has a memory leak.

See the following code:

import Foundation

func go() {
    let x = "adsdasf".dataUsingEncoding(NSUTF8StringEncoding)
    print(x)
}

for i in 0..<1000000 {
    go()
}

Run this code in either Swift 2.1 or 2.2 and watch the activity monitor. Memory usage keeps increasing.

If instead of {"adsdasf".dataUsingEncoding(NSUTF8StringEncoding)} one simply does {NSData()} all seems fine.

So this suggests to me that some memory leak is happening in the dataUsingEncoding(_) function.

I'm not entirely sure if this is Swift related (i.e. goed wrong somewhere in the Objective-C <-> Swift bridging) or if it is purely in Foundation, but I thought to start somewhere 🙂.
If it is Foundation related I should probably file a radar?

@jckarter
Copy link
Member

dataUsingEncoding is returned autoreleased, so it will leak without an autorelease pool formed. Do you still see the leak if you try this?

for i in 0..<1000000 {
  autoreleasepool {
    go()
  }
}

@swift-ci
Copy link
Contributor Author

Comment by Nicky Gerritsen (JIRA)

Ah yes, doing manual memory management in ObjC has been a long time ago, so I completely forgot I had to use autoreleasepools and do this manually when creating console applications.
Will try tonight (CET) if this works and report back.

@swift-ci
Copy link
Contributor Author

Comment by Nicky Gerritsen (JIRA)

Yep autoreleasepool fixed it 🙂

@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
This issue was closed.
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

2 participants