HI,我在NSObject分配时遇到内存泄漏,即
ContactDTO* contactDTO = [[ContactDTO alloc] init];
代码:
+(ContactDTO*) getContactDTOForId:(NSString*) contactId
{
NSString* homeMail =@"";
NSString* workMail=@"";
NSString *lastNameString=@"";
NSString *firstNameString=@"";
firstNameString = [AddressBookUtil getValueForProperty:kABPersonFirstNameProperty forContact:contactId];
lastNameString = [AddressBookUtil getValueForProperty:kABPersonLastNameProperty forContact:contactId];
ABRecordID contactIntId = [contactId intValue];
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, contactIntId);
ABMultiValueRef emailMultiValue =(NSString *)ABRecordCopyValue(person, kABPersonEmailProperty);
for(CFIndex j=0;j<ABMultiValueGetCount(emailMultiValue);j++)
{
NSString* curentTypeLabel =(NSString *)ABMultiValueCopyLabelAtIndex(emailMultiValue,j);
if([curentTypeLabel isEqualToString:@"_$!<Home>!$_"]==YES)
{
NSString* currentEmail =(NSString *)ABMultiValueCopyValueAtIndex(emailMultiValue,j);
if([currentEmail isEqualToString:nil]==NO)
{
homeMail = [currentEmail copy];
}
}
if([curentTypeLabel isEqualToString:@"_$!<Work>!$_"]==YES)
{
NSString* currentEmail =(NSString *)ABMultiValueCopyValueAtIndex(emailMultiValue,j);
if([currentEmail isEqualToString:nil]==NO)
{
workMail = [currentEmail copy];
}
}
}
ContactDTO* contactDTO = [[ContactDTO alloc] init];
contactDTO.firstName = firstNameString;
contactDTO.lastName = lastNameString;
contactDTO.contactId = contactId;
contactDTO.homeEmail = homeMail;
contactDTO.workEmail = workMail;
return [contactDTO autorelease];
}
答案 0 :(得分:1)
从地址簿中读取电子邮件地址时,使用ABMultiValueCopyValueAtIndex()
返回您拥有的引用(例如,您必须使用CFRelease()
发布),[obj copy];
也是如此
我假设您在dealloc方法中发布了homeMail
和workMail
,但此方法中的地址簿中复制的值似乎已泄露。
答案 1 :(得分:1)
您发布了三个与内存泄漏有关的几乎完全相同的问题。阅读Apple's Memory Management Programming Guide可能对您有所帮助。