2011-02-14 1 views
1

iPhone 주소록에서 테이블의 데이터 소스에있는 모든 연락처를로드하도록 요청하는 앱을 빌드하고 있습니다. 다음 코드ABRecordCopyValue에 대한 잠재적 인 메모리 누수

ABAddressBookRef addressBook = ABAddressBookCreate(); 
int nPeople = ABAddressBookGetPersonCount(addressBook); 
CFRelease(addressBook); 

for(int i=0; i < nPeople; i++){ 
    //ABRecordRef person = [allPeople objectAtIndex:i]; 
    NSString *name = @""; 
    if(ABRecordCopyValue([allPeople objectAtIndex:i], kABPersonFirstNameProperty) != NULL) 
     name = [[NSString stringWithFormat:@"%@", ABRecordCopyValue([allPeople objectAtIndex:i], kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
    [dataSource addObject: name]; 
} 

[allPeople release]; 

내가 라인에 대한 잠재적 인 메모리 누수를 얻고 위해

구축 및 분석

을 실행에

name = [[NSString stringWithFormat:@"%@", ABRecordCopyValue([allPeople objectAtIndex:i], kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 

나는 정말 피곤 그것을 고칠 수는 없었습니다. 친절하게 도와주세요.

어떤 종류의 도움이든지 크게 도움이 될 것입니다.

미리 감사드립니다.

답변

4

ABRecordCopyValue의 결과를 공개하지 않습니다. 그것을 변수에 할당하고 루프와 루프의 끝을 놓으십시오. 변수를 사용하면 코드를 훨씬 쉽게 읽고 문제의 원인을 더 잘 강조 할 수 있습니다.

BTW, 당신은 또한 같은 인수로 두 번 ABRecordCopyValue를 호출 만 (위에서 언급 한 바와 같이 변수를 사용하여)을 한 번 수행해야합니다.

+0

덕분에 많이 vickink ...이 날 위해 일했습니다 ... :) ..... 당신은 내 일이 – devsri

+0

야, 너 같이이 저를 도와 주실 수있는 저장 잘 http://stackoverflow.com/questions/4991247/getting-warning-in-setting-delegate-for-abpeoplepickernavigationcontroller thanks – devsri

2

난 당신이 아래와 같이 할 수 있다고 생각 :

CFTypeRef copiedValue = ABRecordCopyValue([allPeople objectAtIndex:i], kABPersonFirstNameProperty); 
name = [[NSString stringWithFormat:@"%@", copiedValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
CFRelease(copiedValue); 
1

당신은 직접있는 NSString에 해소 할 수 있습니다. 그것은 좀 더 명확하게 할 수 있습니다

CFTypeRef fn_typeref = ABRecordCopyValue(person, kABPersonFirstNameProperty); 
CFTypeRef ln_typeref = ABRecordCopyValue(person, kABPersonLastNameProperty); 

NSString * firstName = (__bridge NSString *) fn_typeref; 
NSString * lastName = (__bridge NSString *) ln_typeref; 

NSLog(@"Name:%@ %@", firstName, lastName); 

CFRelease(fn_typeref); // releasing CFTypeRef 
CFRelease(ln_typeref); 

// use firstName and lastName down here 
NSLog(@"Name:%@ %@", firstName, lastName); 
+0

여기 메모리 문제가 될 수 있습니까? 왜냐하면 그 다리가 문자열의 사본을 만들지 못하기 때문입니다. 그러나 그 다음에는 CF 참조를 해제하고 그 후에 NSString을 사용합니다. –