Created
January 29, 2016 03:00
-
-
Save bartcone/8ac22c1b7f05366653c7 to your computer and use it in GitHub Desktop.
Dangling Pointer Playground (weak, unowned)
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 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