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-10308] EXC_BAD_ACCESS crash when capturing self weakly in a convenience initializer #52708

Open
swift-ci opened this issue Apr 4, 2019 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Apr 4, 2019

Previous ID SR-10308
Radar None
Original Reporter dbeard (JIRA User)
Type Bug
Environment

Xcode Version 10.2 (10E125) / Swift 5.0

macOS version 10.14.4 (18E226)

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

md5: b46c020e08aba8bae2389a3ca0cb2b74

Issue Description:

The following code causes a crash at runtime, but compiles fine:

import UIKit


enum FooType {
    case one
    case two
}


typealias Permutation = () -> (Void)


@objcMembers
class CatalogItem: NSObject {


    var t: FooType
    var permutation: Permutation?


    required init(t: FooType) {
        self.t = t
    }
}


@objcMembers
class Subclass: CatalogItem {
    convenience init() {
        self.init(t: .one)


        self.permutation = { [weak self] in
            print(self?.t)
        }
    }
}


class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let s = Subclass() // EXC_BAD_ACCESS !
    }
} 
@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 4, 2019

Comment by Daniel Beard (JIRA)

Note, if I remove the convenience init, remove the 'required' specifier from the superclass and make it a regular init that calls super, there isn't a crash here.

But the compiler shouldn't let me do this.

@belkadan
Copy link
Contributor

belkadan commented Apr 8, 2019

Hm, I think this should be fine. After the self.init is done, you have a fully-built object, and it should be fine to capture that.

@belkadan
Copy link
Contributor

belkadan commented Apr 8, 2019

Reduced to strip out UIKit:

import Foundation

enum FooType {
    case one
    case two
}


typealias Permutation = () -> (Void)


@objcMembers
class CatalogItem: NSObject {


    var t: FooType
    var permutation: Permutation?


    required init(t: FooType) {
        self.t = t
    }
}


@objcMembers
class Subclass: CatalogItem {
    convenience init() {
        self.init(t: .one)


        self.permutation = { [weak self] in
            print(self?.t)
        }
    }
}

do {
    let s = Subclass() // EXC_BAD_ACCESS !
}

@belkadan
Copy link
Contributor

belkadan commented Apr 8, 2019

@mikeash, maybe a runtime issue?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
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. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

3 participants