views:

302

answers:

2

Hi, I am getting leak at:

NSString *firstNameStr = [NSString stringWithFormat:@"%s",firstNameString];

CODE:

+(NSString *)getValueForProperty:(ABPropertyID)propertyId
                      forContact:(NSString *)contactId 
{
    if (addressBook == nil)
    {
        addressBook  = ABAddressBookCreate();
    }
    ABRecordID contactIntId = [contactId intValue];
    ABRecordRef person;
    person = ABAddressBookGetPersonWithRecordID(addressBook, contactIntId);

    CFStringRef firstName;
    char *firstNameString;
    firstName = ABRecordCopyValue(person, propertyId);

    // Paso a char* los datos para que se puedan escribir
    static char* fallback = "";
    int fbLength = strlen(fallback);

    int firstNameLength = fbLength;
    bool firstNameFallback = true;

    if (firstName != NULL)
    {
        firstNameLength = (int) CFStringGetLength(firstName);
        firstNameFallback = false;
    }

    if (firstNameLength == 0)
    {
        firstNameLength = fbLength;
        firstNameFallback = true;
    }

    firstNameString = malloc(sizeof(char)*(firstNameLength+1));
    if (firstNameFallback == true)
    {
        strcpy(firstNameString, fallback);
    }
    else
    {
        CFStringGetCString(firstName, firstNameString,
             10*CFStringGetLength(firstName), kCFStringEncodingASCII);
    }

    if (firstName != NULL)
    {
        CFRelease(firstName);
    }

    NSString *firstNameStr = [NSString stringWithFormat:@"%s",firstNameString];

    free(firstNameString);

    return firstNameStr;
}
+2  A: 

That means that the object allocated at that point is leaked. In this case, most likely because you over-retained it somewhere and failed to release it.

You need to carefully examine the lifespan of that particular string and figure out where you might be overwriting the reference without a release.

Build & Analyze might help considerably.

bbum
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