views:

22

answers:

2

Hi, i've got one question about the memory management of the NSMutableArray. I create an object of my custom class and add this object to a NSMutableArray. Is this automatically retained, so that I can release my created object?

Thanks!

+4  A: 

Yes, it is automatically retained. You should release your object after adding it to the array (or use autorelease)

For example:

NSMutableArray *array = [[NSMutableArray alloc] init];

[array addObject:[[[MyClass alloc] init] autorelease]];

// or

MyClass * obj = [[MyClass alloc] init];

[array addObject:obj];

[obj release];
Philippe Leybaert
+1  A: 

Yes, you can do this for example:

NSMutableArray *array = [[NSMutableArray alloc] init];
NSString *someString = @"Abc";
[array addObject:someString];
[someString release];
NSLog(@"Somestring: %@", [array objectAtIndex:0]);
Tim van Elsloo