【发布时间】:2010-11-22 00:27:58
【问题描述】:
在下面的代码中,PersonListArray 是一个 NSMutableArray,我从 sqlite DB 获取人员列表并将其添加到我的数组中。
Person* tmpPerson = [[Person alloc] init];
tmpPerson.personName = @"Mike";
tmpPerson.personEmail = @"mike@mike.com";
[PersonListArray addObject:tmpPerson];
[tmpPerson release];
即使我在这里释放 Person 对象,它也会造成内存泄漏,我猜这是由于数组持有对它的引用计数。我在程序的其他地方使用了数组,然后确定释放它。
为数组创建新对象而不遇到此问题的最佳做法是什么?
在我释放数组的dealloc方法中
-(void) dealloc{
[PersonListArray release]; // this contains the numerous Person objects
[super dealloc];
}
我应该像这样手动释放它们吗?
-(void) dealloc{
for (int i = 0; i<PersonListArray.count;i++)
{
Person * tmpPerson = [PersonListArray objectAtIndex:i];
[tmpPerson release];
}
[PersonListArray release];
[super dealloc];
}
【问题讨论】:
-
这里有更多的代码来获得更好的图片... @interface Person : NSObject { NSString* personName; NSString* 个人移动; NSString* 个人邮箱; } @property (nonatomic, 保留) NSString* personName, *personEmail, *personMobile;
标签: iphone memory-leaks