Last active
December 16, 2015 20:09
-
-
Save jspahrsummers/5490301 to your computer and use it in GitHub Desktop.
Demonstrating the semantics of __weak objects in Objective-C. See http://clang.llvm.org/docs/AutomaticReferenceCounting.html#semantics for more information.
This file contains hidden or 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
// The weak pointer is read, and retained until the message send completes its synchronous work. | |
[weakValue doAThing]; |
This file contains hidden or 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
- (void)doAThing { | |
// Within here, 'self' is __strong. | |
__unsafe_unretained selfCopy = self; | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
// 'selfCopy' may be garbage here. | |
[selfCopy doAnotherThing]; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Second one is obviously bad, but what about this: