开发者

Memory still increasing when use CFRelease for ABRecordCopyValue?

i have a problem make headache, 开发者_高级运维i simply create method:

-(void) main{

      for (int i = 0; i< 100;i++) {
           [self getPhoneOfContact:i];
      }
 }

-(void)getPhoneOfContact:(NSInteger)id_contact {

     ABRecordRef record = ABAddressBookGetPersonWithRecordID(addressBook,id_contact);

     CFTypeRef ref1;
     ref1 = ABRecordCopyValue(record,kABPersonPhoneProperty);

     CFRelease(record);
     CFRelease(ref1); 
}

I think the memory will approximate constants because i have release memory copied, but in reality it still increasing for each loop i; who can explain me this :(. thanks!


Your code is wrong. The ABAddressBookGetPersonWithRecordID call follows the Core Foundation 'Get Rule'. This means you do not have ownership of the return value and thus you do not have to release it.

See Core Foundation - Memory Management


It's possible that running ABAddressBookGetPersonWithRecordID and/or ABRecordCopyValue uses some autoreleased objects you don't know about, and this would cause the memory to increase. It will be released, though, when the nearest autorelease pool is drained. This is not a problem with your application.

Try to place the loop in its own autorelease pool and drain the pool after you execute the loop:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// do computation
[pool release];

Does this "reset" the memory to the initial amount?

Also, in general, try to use XCode's Instruments Memory Leaks instead of looking at memory usage - this can be only helpful, but Memory Leaks is the main tool to find, well.. memory leaks :)


I'm not sure, but I think this may be what's happening...

 // 1. alloc and return a record address to record
 ABRecordRef record = ABAddressBookGetPersonWithRecordID(addressBook,id_contact);
 // 2. alloc and return the default CFTypeRef to ref1
 CFTypeRef ref1;
 // 3. copy and return the value of kABPersonPhoneProperty
 ref1 = ABRecordCopyValue(record,kABPersonPhoneProperty);
 // release 1
 CFRelease(record);
 // release 3
 CFRelease(ref1);

Thus, 2 is still leaking. Maybe try doing CFTypeRef ref1 = nil or just doing it all on one line?


Does this even work for you? The actual retrieval of the info? This is what I use to retrieve the kABPersonPhoneProperty out of records.

ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
    for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++) {
        CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex(multi, i);
        CFStringRef phoneNumber      = ABMultiValueCopyValueAtIndex(multi, i);
                  // Do stuff with the info.
        CFRelease(phoneNumberLabel);
        CFRelease(phoneNumber);
    }
    CFRelease(multi);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜