Cannot reference unowned object conforming to protocol

Originator:ash.furrow
Number:rdar://17569045 Date Originated:July 6, 2014
Status:Open Resolved:No
Product:Developer Tools Product Version:Xcode 6 Beta 2
Classification:Serious Bug Reproducible:Always
 
If I have a protocol, and an object that conforms to that protocol, and I have another object that has an unowned reference to an object conforming to that protocol, after setting that reference in init, accessing it crashes my app. 

So.

That's a protocol:

@objc protocol MyProtocol: NSObjectProtocol {
    func method() -> ()
}

And an object conforming to that protocol:

class DelegateClass: NSObject, MyProtocol {
    func method () {
        for i in 1...10 {
            "Hello!"
        }
    }
}

So far so good. Then I have an object that has a reference to an object conforming to that protocol which is unowned. 

class MyObject : NSObject {
    unowned let delegate: MyProtocol
    
    init(delegate: MyProtocol) {
        self.delegate = delegate
        super.init()
    }
    
    func doSomethingWithDelegate() {
        delegate.method()
    }
}

OK, cool. Now let's set some instances up:

let delegate = DelegateClass()
let object = MyObject(delegate: delegate)

And try and access the delegate:

object.doSomethingWithDelegate()

Crash. 

* thread #1: tid = 0x8f817d, 0x001ba570 libswift_stdlib_core.dylib`_swift_release_slow, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
    frame #0: 0x001ba570 libswift_stdlib_core.dylib`_swift_release_slow
    frame #1: 0xbfffcf98
    frame #2: 0x001c8f7e libswift_stdlib_core.dylib`swift_unknownRetainUnowned + 318
    frame #3: 0x00003470 Bug`@objc Bug.MyObject.delegate.getter : @unowned Bug.MyProtocol + 64 at AppDelegate.swift:0
  * frame #4: 0x0000364c Bug`Bug.MyObject.doSomethingWithDelegate (self=0x0b20ff70)() -> () + 60 at AppDelegate.swift:29
    frame #5: 0x000036c2 Bug`@objc Bug.MyObject.doSomethingWithDelegate (Bug.MyObject)() -> () + 34 at AppDelegate.swift:0
    frame #6: 0x00003e95 Bug`Bug.AppDelegate.application (application=0x018fe804, launchOptions=None, self=0x0b500040)(ObjectiveC.UIApplication, didFinishLaunchingWithOptions : Swift.Optional<ObjectiveC.NSDictionary>) -> Swift.Bool + 149 at AppDelegate.swift:56
    frame #7: 0x00003f55 Bug`@objc Bug.AppDelegate.application (Bug.AppDelegate)(ObjectiveC.UIApplication, didFinishLaunchingWithOptions : Swift.Optional<ObjectiveC.NSDictionary>) -> Swift.Bool + 101 at AppDelegate.swift:0
    frame #8: 0x00c44b37 UIKit`-[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 291
    frame #9: 0x00c45875 UIKit`-[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2920
    frame #10: 0x00c48a33 UIKit`-[UIApplication _runWithMainScene:transitionContext:completion:] + 1507
    frame #11: 0x00c60eb8 UIKit`__84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke + 59
    frame #12: 0x00c4777e UIKit`-[UIApplication workspaceDidEndTransaction:] + 29
    frame #13: 0x03404f1f FrontBoardServices`-[FBSWorkspace clientEndTransaction:] + 87
    frame #14: 0x0340c4ed FrontBoar


Source: http://cloud.ashfurrow.com/1h05282t000X

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!