Created
July 2, 2014 03:01
-
-
Save gregawoods/7bbf2587bf5bbe241ff6 to your computer and use it in GitHub Desktop.
Demonstrate an EXC_BAD_ACCESS error when attempting to use optional chaining on a weak delegate variable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Playground - noun: a place where people can play | |
import Cocoa | |
@class_protocol protocol DownloaderDelegate { | |
func finishedDownloading(fileName: String) | |
} | |
class Downloader { | |
weak var delegate: DownloaderDelegate? | |
func download(fileName: String) { | |
delegate?.finishedDownloading(fileName) | |
} | |
} | |
class Owner: DownloaderDelegate { | |
func finishedDownloading(fileName: String) { | |
println("All done! \(fileName)") | |
} | |
} | |
let owner = Owner() | |
let downloader = Downloader() | |
downloader.delegate = owner | |
downloader.download("lolcat.png") | |
/* | |
Playground execution failed: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=2, address=0x1074df490). | |
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation. | |
* thread #1: tid = 0xa5cc5e, 0x00000001074df490, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x1074df490) | |
* frame #0: 0x00000001074df490 | |
frame #1: 0x00000001074d78c1 | |
frame #2: 0x00000001072eb6b0 libswift_stdlib_core.dylib`_TtCSs13_NSSwiftArray + 128 | |
frame #3: 0x00000001072eb6d8 libswift_stdlib_core.dylib`SwiftObject + 40 | |
*/ |
Also notable: Replacing @class_protocol
with @objc
on the protocol declaration clears up the issue entirely, as per this StackOverflow comment.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Error goes away when declaring the delegate as strong. This feels like a bug to me.