- (NSArray *) makeKeyValueArray: (NSArray *) arr
{
NSMutableArray *result = [[NSMutableArray alloc] init];
for(int i = 0; i < [arr count]; i++)
{
[result addObject:[[KeyValue alloc] initWithData:[arr objectAtIndex:i] :[arr objectAtIndex:i]]];
}
return result;
}
仪器在上面的代码中显示188个泄漏,为什么?有人可以向我解释一下吗?
答案 0 :(得分:5)
- (NSArray *) makeKeyValueArray: (NSArray *) arr
{
NSMutableArray *result = [[NSMutableArray alloc] init];
for(int i = 0; i < [arr count]; i++)
{
id obj = [[KeyValue alloc] initWithData:[arr objectAtIndex:i] :[arr objectAtIndex:i]]; // obj reference count is now 1, you are the owner
[result addObject:obj]; //reference count is now 2, the array is also an owner as well as you.
[obj release];// reference count is now 1, you are not the owner anymore
}
return [result autorelease];
}
查看Basic Memory Management Rules
您必须放弃您拥有的对象的所有权