We both know that the retain method will +1 retainCount, release will -1 retainCount, if retainCount == 0, the object dealloc. But I meet a problem, the following code run have the ridiculous result
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property(nonatomic, retain) NSString *name;
@property(nonatomic, assign) NSInteger age;
@end
@implementation Person
- (id)init
{
self = [super init];
if (self) {
self.name = @"name";
self.age = 20;
}
return self;
}
- (void)dealloc
{
NSLog(@"dealloc");
[super dealloc];
}
@end
int main(int argc, const char * argv[])
{
Person *per1 = [[Person alloc] init];
NSLog(@"retainCount = %lu -- 1",[per1 retainCount]);
[per1 release];
[per1 retain];
NSLog(@"retainCount = %lu -- 2",[per1 retainCount]);
return 0;
}
The result is:
2014-01-11 21:56:23.887 blockTest[1287:303] retainCount = 1 -- 1
2014-01-11 21:56:23.889 blockTest[1287:303] dealloc
2014-01-11 21:56:23.889 blockTest[1287:303] retainCount = 2 -- 2
I don't use the ARC. Why I get this result? Isn't the app should crash?
You shouldn't ever expect that this property returns any meaningful result.
It doesn't matter what you do, it's not your job.
See this for more information whentouseretaincount.com