swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.27k stars 10.33k forks source link

[SR-4180] Swift Leak even when unowned is used with Protocol in small code example #46763

Open swift-ci opened 7 years ago

swift-ci commented 7 years ago
Previous ID SR-4180
Radar None
Original Reporter patchthecode (JIRA User)
Type Bug

Attachment: Download

Environment XCode 8, swift 3, OS Sierra, iOS 7plus Simulator, iOS 10.3
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: b076ea3ea4c599909691dc74ed03b68d

Issue Description:

Here is my code: I have the ViewWithCollectionView on my story board. It's just a simple empty view created for this example.

I have set my delegate as unowned, but Xcode Leaks still show this as a leak, and I do not understand why.

import UIKit

class ViewController: UIViewController  {
    @IBOutlet weak var theView: ViewWithCollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

class ViewWithCollectionView: UICollectionView, SomeProtocol {
    override func awakeFromNib() {
        collectionViewLayout = SomeLayout(with: self)
    }
}

open class SomeLayout: UICollectionViewLayout, SomeProtocol {
    unowned var delegate: SomeProtocol

    init(with delegate: SomeProtocol) {
        self.delegate = delegate // Leak happens here
        super.init()
    }
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

protocol SomeProtocol: class {}
belkadan commented 7 years ago

I suspect this is the same issue we had with weak. @jckarter, @slavapestov?

jckarter commented 7 years ago

I don't recall what that issue was. Got a link, @belkadan?