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-9982] Memory leak when casting Array of AnyObjects ([AnyObject]) to [AnyHashable] #52386

Closed
swift-ci opened this issue Feb 23, 2019 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. runtime The Swift Runtime

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-9982
Radar None
Original Reporter carboncomputed (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug, Runtime
Assignee None
Priority Medium

md5: 8aa68f4906fb7f595745de90e6eea168

Issue Description:

https://pastebin.com/xaQ94TWb

The cast succeeds but deinit is never called and our objects are never deallocated. This also happens if I compactMap through the array and try to add each one individually.

import UIKit


class Test: Hashable {
 static func == (lhs: Test, rhs: Test) -> Bool {
 return lhs.str == rhs.str
 }
 
 deinit {
 print("hello!")
 }
 
 func hash(into hasher: inout Hasher) {
 hasher.combine(str)
 }
 
 var str: String = "test"
}

class ViewController: UIViewController {

override func viewDidLoad() {
 super.viewDidLoad()
 var t = Test()
 var t2 = Test()
 var values: [AnyObject] = [t,t2]
 
 let hashable: [AnyHashable] = values as! [AnyHashable]
 
 // Do any additional setup after loading the view, typically from a nib.
 }


}
@belkadan
Copy link
Contributor

@Catfish-Man, @mikeash, does this sound familiar?

carboncomputed (JIRA User), what version of Swift are you using?

@Catfish-Man
Copy link
Member

Can't say it rings a bell, no

@mikeash
Copy link
Contributor

mikeash commented Feb 25, 2019

Sounds similar to the bug I fixed in #20836. Probably the same sort of ownership mismatch in the casting code.

@swift-ci
Copy link
Collaborator Author

Comment by Kevin Carbone (JIRA)

@belkadan I'm using Swift 4.2.

Note this also occurs for me if I try to iterate through values (compactMap, for loop, etc) and try to cast on the initial values.

@mikeash
Copy link
Contributor

mikeash commented Feb 25, 2019

Can you check with the Xcode 10.2 beta (or other 5.0 of your choice) and see if it happens there? Could be that it actually is that problem I fixed above.

If it's still present, can you see if a single AnyObject -> AnyHashable cast also leaks? If the array is superfluous that would help narrow it down.

@swift-ci
Copy link
Collaborator Author

Comment by Kevin Carbone (JIRA)

@mikeash I've confirmed that I no longer see this leak on Swift 5. It looks to be fixed by your patch! Awesome. Are there any limitations to releasing my app packaged with Swift 5 even though its Beta? (Apple rejection, etc)?

@mikeash
Copy link
Contributor

mikeash commented Feb 26, 2019

Glad to hear it! I don't think it's recommended to distribute an app built with a beta Xcode. Obviously, non-beta releases are not exactly guaranteed to be bug-free either, but it's best to wait if you can.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. runtime The Swift Runtime
Projects
None yet
Development

No branches or pull requests

4 participants