Person *p = [[Person alloc] init]; // P 1
NSUInteger c = [p retainCount];
NSLog(@"计数器:%ld",c);
[p retain]; //p 2
c = [p retainCount];
NSLog(@"计数器:%ld",c);
[p release]; //p 1
c = [p retainCount];
NSLog(@"计数器:%ld",c);
[p release]; //野指针 这里p的引用应该为0 才是,但是还是1,必须将p =nil,这时 p retainCount的值才是0
c = [p retainCount];
NSLog(@"计数器:%ld,%p",c,p);
//p.age = 10;对象已经被释放,将出现内存错误 |
|