这个复制代码是否有泄漏?

时间:2011-01-10 03:49:07

标签: iphone memory-management memory-leaks

此代码是否有泄漏?

// Move the group
 Group *movedGroup = [[Group alloc] init];
 movedGroup = [[[[GroupList sharedGroupList] groups] objectAtIndex:fromIndex] copy];
 [[GroupList sharedGroupList] deleteGroup:fromIndex];
 [[GroupList sharedGroupList] insertGroup:movedGroup atIndex:toIndex];

 // Update the loadedGroupIndex pointer
 if (loadedGroupIndex < fromIndex & loadedGroupIndex >= toIndex) {
  loadedGroupIndex = loadedGroupIndex + 1;
 } else if (loadedGroupIndex > fromIndex & loadedGroupIndex < toIndex) {
  loadedGroupIndex = loadedGroupIndex - 1;
 } else if (loadedGroupIndex == fromIndex) {
  loadedGroupIndex = toIndex;
 }
 [movedGroup release]

1 个答案:

答案 0 :(得分:2)

第一个实例化的组将泄漏。您丢失了对它的引用(分配movedGroup = ...)并且没有标记为自动释放。您可以将前两行减少到:

Group *movedGroup = [[[[GroupList sharedGroupList] groups] objectAtIndex:fromIndex] copy];

它会做同样的事情,没有泄漏。