Skip to content

Instantly share code, notes, and snippets.

@bartcone
Created January 29, 2016 03:00
Show Gist options
  • Save bartcone/8ac22c1b7f05366653c7 to your computer and use it in GitHub Desktop.
Save bartcone/8ac22c1b7f05366653c7 to your computer and use it in GitHub Desktop.
Dangling Pointer Playground (weak, unowned)
//: Playground - noun: a place where people can play
import UIKit
class Test {
let handler: () -> ()
deinit {
print("dealloc'd")
}
init(withHandler handler: () -> ()) {
self.handler = handler
}
func something() {
handler()
print("before")
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { [unowned self] in
print("\(unsafeAddressOf(self)) wwdsfdsfdsfd")
self.somethingElse()
}
print("after")
}
func somethingElse() {
print("\(unsafeAddressOf(self)) Hmm")
}
}
class SomewhereElse {
var t: Test?
func test() {
t = Test(withHandler: { () -> () in
print(unsafeAddressOf(self.t!))
self.t = nil
})
t!.something()
}
}
let s = SomewhereElse()
s.test()
print("done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment