lu yuan
lu yuan

Reputation: 7227

NSMutableArray leaks warning addressed by the Instrument

I am using Instrument leaks tools to check leaks in the code.

//MyClass.h
@property (nonatomic, retain) NSMutableArray *marrProperty;

//MyClass.m
NSSortDescriptor *createdTime = [[NSSortDescriptor alloc] initWithKey:@"createdTime" ascending:NO selector:@selector(compare:)];             
NSArray *sortedArray = [self.anManagedObj.aRelationships sortedArrayUsingDescriptors:[NSArray arrayWithObject:createdTime]];
[createdTime release];
NSMutableArray *marr = [[NSMutableArray alloc] initWithArray:sortedArray];
self.marrProperty = marr;
[marr release];

After checking with the leaks tool in Instrument, I was told the leaks happened in the following code:

NSMutableArray *marr = [[NSMutableArray alloc] initWithArray:sortedArray];
self.marrProperty = marr;

I don't know why, because I just alloc and release well.

Upvotes: 0

Views: 189

Answers (2)

Dustin Howett
Dustin Howett

Reputation: 1635

Have you implemented a custom setter for marrProperty? This could be a source of memory issues.

Additionally, you might prefer to use [[sortedArray mutableCopy] autorelease], instead of initWithArray: followed by a release. Simply for code clarity.

If you're not using ARC, make sure that you release marrProperty in -dealloc for that class.

Upvotes: 1

bbum
bbum

Reputation: 162722

Instruments is showing you were the leaked object was allocated, not where it was leaked.

You need to find the extra retain. You can use Instruments to do that; the Allocations instrument can be configured to track retain/release events.

This will likely be helpful.

Upvotes: 4

Related Questions