Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
@interface Test : NSObject
@implementation Test
- (void)dealloc {
NSLog(@"Deallocated %@", self);
@interface Holder : NSObject
@implementation Holder {
Test *strongIvar;
__unsafe_unretained Test *unsafeIvar;
- (void)test {
Holder *holder = [[Holder alloc] init];
NSLog(@"Setting strong");
Ivar strongIvar = class_getInstanceVariable([Holder class], "strongIvar");
object_setIvar(holder, strongIvar, [[Test alloc] init]);
NSLog(@"Setting unsafe");
Ivar unsafeIvar = class_getInstanceVariable([Holder class], "unsafeIvar");
object_setIvar(holder, unsafeIvar, [[Test alloc] init]);
NSLog(@"Done with %@", holder);

jder commented Mar 2, 2013

This produces the following output:

2013-03-02 13:30:41.456 TEST[44782:c07] Setting strong
2013-03-02 13:30:41.457 TEST[44782:c07] Setting unsafe
2013-03-02 13:30:41.457 TEST[44782:c07] Deallocated <Test: 0x8834fa0>
2013-03-02 13:30:43.666 TEST[44782:c07] Done with <Holder: 0x886eec0>
2013-03-02 13:30:45.044 TEST[44782:c07] Deallocated <Test: 0x7152070>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment