I used the leaks tool in Instruments to test the code, but the leaks tool cannot seem to find the leak.
At the end of my code, the output of NSLog(@"str count:%d",[str retainCount]);
is 3. Why? I don't override the dealloc. [a.name retainCount] is there just one time
and I only autorelease str for one time. So str shouldn't leak.
@interface DataMode : NSObject {
NSString * name;
}
@property (retain) NSString * name;
- initWithName:(NSString * )name_;
@end
@implementation DataMode
@synthesize name;
- initWithName:(NSString * )name_
{
if ([super init] != nil)
{
name = name_;
return self;
}
return nil;
}
@end
- (void) pressed:(id)sender
{
for( int i = 0;i<10000000;i++)
{
NSString * str = [NSString stringWithFormat:@"zhang"];
DataMode * a = [[DataMode alloc] initWithName:str];
NSLog(@"a0 count:%d",[a retainCount]);
NSLog(@"name1 count:%d",[a.name retainCount]);
NSLog(@"name1 count:%d",[a.name retainCount]);
NSLog(@"a1 count:%d",[a retainCount]);
[ a release];
NSLog(@"str count:%d",[str retainCount]);
NSLog(@"str count:%d",[str retainCount]);
}
}
@end