First of all, sorry for the amount of code.
What i’m doing wrong managing memory. I can’t understand why the analyzer throws a memory leak.
@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;
}
Easy fix, you forgot to release the existing value before reassigning it.
You can get around this completely by using numberWithFloat in both instances which returns an autoreleased object.
Or you can fix your existing example by: