首先,对于代码量,感到抱歉。 我在做什么错误管理内存。我不明白为什么分析器会引发内存泄漏。简单示例代码中的内存泄漏
@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