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-6895] Timer property with initializer and self target causes crash at runtime #49444

Closed
mattneub opened this issue Feb 1, 2018 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation duplicate Resolution: Duplicates another issue expressions Feature: expressions identifiers Feature: Identifiers missing warning Bug: Missing warning self Feature → expressions: The 'self' expression swift 5.6 type checker Area → compiler: Semantic analysis

Comments

@mattneub
Copy link

mattneub commented Feb 1, 2018

Previous ID SR-6895
Radar None
Original Reporter @mattneub
Type Bug
Status Resolved
Resolution Duplicate
Environment

Swift 4.1, Xcode 9.3 beta 1

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

md5: b788bbb198c95ec64db19b32b0652bd1

duplicates:

Issue Description:

I thought I had raised this issue before, but now I see that I never made it explicit, so I'd like this code on the record:

class ViewController: UIViewController {
    var timer : Timer? = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerFired), userInfo: nil, repeats: true)
    @objc func timerFired(_ : Timer) {
        print("fired")
    }
}

That code is legal (the compiler doesn't complain) but causes a crash at runtime. The reason seems to be that the self referred to as the target is not the view controller (presumably because our object is not fully formed the time we are initializing properties).

This is a very common trap for beginners to fall into. The compiler should disallow this use of self.

This may be a duplicate of #47442 but I was not so explicit there about the Timer problem, which is the common use case that bites everyone.

@belkadan
Copy link
Contributor

belkadan commented Feb 2, 2018

Yeah, I don't think we get enough benefit from tracking this separately.

@belkadan
Copy link
Contributor

belkadan commented Feb 2, 2018

I feel like it's only a "very common trap" if you actually define the timer in a property's initial value. Is that really such a common thing to do?

@mattneub
Copy link
Author

mattneub commented Feb 2, 2018

@belkadan It's plenty common. I earn some healthy Stack Overflow rep. from telling people not to do it and why they are crashing when they do.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added unexpected behavior Bug: Unexpected behavior or incorrect output swift 4.1 missing warning Bug: Missing warning type checker Area → compiler: Semantic analysis expressions Feature: expressions diagnostics QoI Bug: Diagnostics Quality of Implementation duplicate Resolution: Duplicates another issue identifiers Feature: Identifiers self Feature → expressions: The 'self' expression swift 5.6 and removed unexpected behavior Bug: Unexpected behavior or incorrect output swift 4.1 labels May 2, 2023
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. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation duplicate Resolution: Duplicates another issue expressions Feature: expressions identifiers Feature: Identifiers missing warning Bug: Missing warning self Feature → expressions: The 'self' expression swift 5.6 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants