NSCFString泄漏inVolving NSString

时间:2010-03-23 07:10:54

标签: objective-c memory-leaks

我在泄密:

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;
}

1 个答案:

答案 0 :(得分:4)

这意味着此时分配的对象泄露。在这种情况下,很可能是因为你在某处过度保留了它而未能release它。

您需要仔细检查该特定字符串的生命周期,并找出在没有发布的情况下覆盖引用的位置。

构建&分析可能会有很大帮助。