首先,對於代碼量,感到抱歉。 我在做什麼錯誤管理內存。我不明白爲什麼分析器會引發內存泄漏。簡單示例代碼中的內存泄漏
@interface obj : NSObject
{
NSMutableArray *array;
}
@property (retain, nonatomic) NSMutableArray *array;
@end
@implementation obj
@synthesize array;
- (id)init
{
self = [super init];
if (self)
{
// Initialization code here.
array = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc
{
[super dealloc];
[array release];
}
@end
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
obj *test = [[obj alloc] init];
NSNumber *numero = [[NSNumber alloc] initWithFloat:3.4];
NSLog(@"Número: %g \n", [numero floatValue]);
[test.array addObject:numero];
NSLog(@"Numero de elementos: %lu", [test.array count]);
NSLog(@"Valor: %g", [[test.array objectAtIndex:0] floatValue]);
NSLog(@"Numero de elementos t2: %lu", [test.array count]);
numero = [NSNumber numberWithFloat:5.8];
NSLog(@"Valor t2: %g", [[test.array objectAtIndex:0] floatValue]);
NSLog(@"Número t2: %g \n", [numero floatValue]);
[test.array addObject:numero];
NSLog(@"Valor: %g", [[test.array objectAtIndex:0] floatValue]);
[numero release]; **<-- Leak of memory**
[test release];
[pool drain];
return 0;
}
感謝您的幫助和很好的解釋:
或者您也可以通過修復現有的例子。我不知道humberWithFloat的方法是自動發佈的。 – 2011-12-15 22:01:54