2010-03-23 4 views
1

METTANT EN JEU Je me fais fuite à:fuite NSCFString NSString

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

Il semble que la plupart de cette méthode convertit un CFStringRef en NSString. Ce n'est pas vraiment nécessaire. Tout ce que vous avez à faire est de transtyper CFStringRef en NSString. Le pont sans frais de Cocoa permet d'utiliser les deux de manière interchangeable. Vous devez vous assurer que la valeur renvoyée par ABRecordCopyValue est réellement une chaîne. Vous pouvez utiliser 'CFGetTypeID (firstName) == CFStringGetTypeID()' pour vous assurer que la valeur renvoyée est CFStringRef, puis vous pouvez la convertir en NSString avec 'NSString * value = (NSString *) firstName'. –

Répondre

4

Cela signifie que l'objet alloué à ce moment est une fuite. Dans ce cas, le plus probable parce que vous l'avez sur-retenu quelque part et échoué à release.

Vous devez examiner attentivement la durée de vie de cette chaîne particulière et déterminer où vous risquez d'écraser la référence sans publication.

Construire & L'analyse peut aider considérablement.