Reputation: 3036
I know this might be a simple question but I have to make sure that I'm going well on managing memory. I have the following code in .h file:
@interface Class1 : NSObject {
NSArray * _Objects;
}
@property (retain) NSArray * Objects;
@end
and the following code in .m file:
@implementation Class1
@synthesize Objects = _Objects;
#pragma mark -
#pragma mark Initializers
- (id)init {
self = [super init];
if (self) {
NSMutableArray * objects = [[NSMutableArray alloc] init];
NSObject * object1 = [[NSObject alloc] init];
NSObject * object2 = [[NSObject alloc] init];
NSObject * object3 = [[NSObject alloc] init];
[objects addObject:object1];
[objects addObject:object2];
[objects addObject:object3];
[object1 release];
[object2 release];
[object3 release];
}
return (self);
}
#pragma mark -
#pragma mark Some Method
- (void)method {
for (NSObject * object in self.Objects) {
// do some thing
[object release]; // (1) ?
}
NSObject * object = [self.Objects objectAtIndex:0];
[object release]; // (2) ?
for (NSObject * object in _Objects) {
// do some thing
[object release]; // (3) ?
}
NSObject * object = [_Objects objectAtIndex:0];
[object release]; // (4) ?
}
and my question are all the release calls are correct (from 1 to 4) and if not what is right and what is not. or another phrase when the NSArray (given the fact that it has been initialized with an NSMutableArray) retains the objects and when it doesn't?
thanks in advance.
Upvotes: 0
Views: 63
Reputation: 28590
None of those calls are correct. You should only release
objects that you have allocated or retained.
Upvotes: 2
Reputation: 181270
No. You should not release it. A simple rule to follow is:
If you didn't allocc or retain the object, then do not release it.
Upvotes: 2