Skip to content

Instantly share code, notes, and snippets.

Created October 28, 2014 21:51
Show Gist options
  • Save anonymous/a349ad59282d9770bb64 to your computer and use it in GitHub Desktop.
Save anonymous/a349ad59282d9770bb64 to your computer and use it in GitHub Desktop.
#import "AppDelegate.h"
#import <objc/runtime.h>
static NSString *Key = @"Key";
@interface A : NSObject
@property (strong, nonatomic) NSString *property;
@end
@implementation A
- (id)init
{
if (self = [super init]) {
self.property = @"Live Hard, Crash Harder.";
}
return self;
}
- (void)dealloc
{
NSLog(@"Dealloc a. property is: %@", self.property);
}
@end
@interface B : NSObject
@property (weak, nonatomic) A *weakA;
@property (assign, nonatomic) A *unsafeA;
@end
@implementation B
- (void)dealloc
{
NSLog(@"Dealloc b. Weak a: %@, unsafe a: %@, unsafe a's property: %@, unsafe a's class: %@", self.weakA, self.unsafeA, self.unsafeA.property, self.unsafeA.class);
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
A *a = [[A alloc] init];
B *b = [[B alloc] init];
b.weakA = a;
b.unsafeA = a;
objc_setAssociatedObject(a, &Key, b, OBJC_ASSOCIATION_RETAIN);
NSLog(@"Releasing b");
b = nil;
NSLog(@"Releasing a");
a = nil;
NSLog(@"Done");
// Prints the following:
//
// Releasing b
// Releasing a
// Dealloc a. property is: Live Hard, Crash Harder.
// Dealloc b. Weak a: (null), unsafe a: <A: 0x7f8513d13fc0>, unsafe a's property: (null), unsafe a's class: A
// Done
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment