Marian André
Marian André

Reputation: 196

Objective-C Getter Memory Management

I'm fairly new to Objective-C and am not sure how to correctly deal with memory management in the following scenario:

I have a Core Data Entity with a to-many relationship for the key "children". In order to access the children as an array, sorted by the column "position", I wrote the model class this way:

@interface AbstractItem :  NSManagedObject  
{
    NSArray * arrangedChildren;
}

@property (nonatomic, retain) NSSet * children;
@property (nonatomic, retain) NSNumber * position;
@property (nonatomic, retain) NSArray * arrangedChildren;

@end


@implementation AbstractItem

@dynamic children;
@dynamic position;
@synthesize arrangedChildren;

- (NSArray*)arrangedChildren
{
    NSArray* unarrangedChildren = [[self.children allObjects] retain];
    NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"position" ascending:YES];
    [arrangedChildren release];
    arrangedChildren = [unarrangedChildren sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    [sortDescriptor release];
    [unarrangedChildren release];
    return [arrangedChildren retain];
}

@end

I'm not sure whether or not to retain unarrangedChildren and the returned arrangedChildren (first and last line of the arrangedChildren getter). Does the NSSet allObjects method already return a retained array? It's probably too late and I have a coffee overdose.

I'd be really thankful if someone could point me in the right direction. I guess I'm missing vital parts of memory management knowledge and I will definitely look into it thoroughly.

Upvotes: 0

Views: 285

Answers (2)

Georg Fritzsche
Georg Fritzsche

Reputation: 99074

-allObjects returns an autoreleased instance, there is no need to retain and release it.

As for arrangedChildren, it will be retained only if you use the synthesized setter:

self.arrangedChildren = [unarrangedChildren sortedArrayUsingDescriptors:/*...*/];

Directly assigning to the instance variable as you do does not invoke the synthesized setter.

Finally, you shouldn't retain the return value here - your method isn't named starting with alloc, new or create and callers thus have to take ownership explicitly.

I recommend reading the "Cocoa Memory Management Guide" and the section on "Declared Properties" in the Objective-C language description.

Upvotes: 3

Peter Zich
Peter Zich

Reputation: 512

The retain in your @property for arrangedChildren should take care of that. You will need to release it in your dealloc.

Upvotes: 0

Related Questions