views:

113

answers:

2

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;
contactDTO.contactId = contactId;
contactDTO.homeEmail = homeMail;
contactDTO.workEmail = workMail;

return [contactDTO autorelease];
}
+1  A: 

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.

frenetisch applaudierend
I done it as of ur suggestion but still Im getting Memory leak at same place.can u give any other suggestions to get out of this???
Srilakshmi Manthena
After a second look on your example I see that the `[currentEmail copy];` you're doing is also overhead, just assign them (e.g. `workMail = currentEmail;`). But I agree with Isaac, you should definitively read the Memory Management Programming Guide as such topics are covered there.
frenetisch applaudierend
+1  A: 

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.

Isaac