swiftlang / swift

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

[SR-4682] Object deallocated with a specific combination of protocol inheritance and a switch clause #47259

Open swift-ci opened 7 years ago

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

Attachment: Download

Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, RunTimeCrash | |Assignee | None | |Priority | Medium | md5: 937be89180ecc6337d301e9a32716c54

relates to:

Issue Description:

Running the following lines of code will reproduce the issue:

import Foundation
public protocol SomeObjectProtocol : NSObjectProtocol { }
public protocol BaseProtocol { }
public protocol SubProtocol: BaseProtocol, SomeObjectProtocol { }
class SomeClass: NSObject, SubProtocol { }

func doTheSwitch(_ item: BaseProtocol) {
    switch item {
    case let _ as SubProtocol:
        break
    default:
        break
    }
}

let items: [BaseProtocol] = [SomeClass()]
doTheSwitch(items[0])
doTheSwitch(items[0])
print("\(items)")

This is reproducible on both, macOS and iOS. From what it looks, the following has to happen here:

After this, with zombies enabled you get something like this:

[RetainReleaseIssue.SomeClass retain]: message sent to deallocated instance 0x60800000a99

Attached is an example of the issue which is also defined here.

swift-ci commented 7 years ago

Comment by Mikko Välimäki (JIRA)

Looks like the issue could be even reduced a bit. The issue might be related to something mentioned here: https://github.com/lionheart/openradar-mirror/issues/12033

belkadan commented 7 years ago

@jckarter, we've seen this before, correct?

belkadan commented 7 years ago

Seems to be fixed on master, which is good. As a workaround, if it's the issue I'm thinking of I believe it does not occur when you use chained if instead of switch.