开发者

Memory leak at NSObject allocation

HI, I am getting memory leak at NSObject allocation i.e.,

ContactDTO* contactDTO = [[ContactDTO alloc] init];

Code:

+(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;
contact开发者_运维问答DTO.contactId = contactId;
contactDTO.homeEmail = homeMail;
contactDTO.workEmail = workMail;

return [contactDTO autorelease];
}


When reading the email addresses from the Address Book you use ABMultiValueCopyValueAtIndex() which returns a reference which is owned by you (e.g. must be released using CFRelease() by you), as does [obj copy];.

I assume you release the homeMail and workMail in your dealloc method but the copied value from the address book seems leaked in this method.


You've posted three nearly-identical questions pertaining to memory leaks. It might be helpful for you to read through Apple's Memory Management Programming Guide.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜