I've gone crazy trying to find a leak with an NSMutableArray:

NSMutableArray *mutablearray =[[[[NSMutableArray alloc] initWithArray: array] mutableCopy] autorelease];

Finally I understood that I need to autorelease twice my mutablearray because initWithArray is +1 and mutableCopy is +1 too.

Then I'm doing:

NSMutableArray *mutablearray = [[[NSMutableArray alloc] initWithArray: array] autorelease];
mutablearray = [[mutablearray mutableCopy] autorelease];

But, it's correct to do?:

NSMutableArray *mutablearray = [[[[[NSMutableArray alloc] initWithArray: array] autorelease] mutableCopy] autorelease];

Thanks

有帮助吗?

解决方案

The first question comes in mind is WHY you are initializing and also making a mutable copy at the same time initWithArray gives a new instance with new memory and you can use it.Then why creating a mutable copy of it?

Use

NSMutableArray *mutablearray = [[[NSMutableArray alloc] initWithArray: array]autorelease];

This gives you a mutable instance .So no need for calling mutablecopy anyway

OR

NSMutableArray *mutablearray = [[array mutableCopy]autorelease];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top