//get instance of shared addressbook
ABAddressBookRef addressBook = ABAddressBookCreate();
//get all contacts in addressbook
CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numPeople = ABAddressBookGetPersonCount(addressBook);
self.contacts = [NSMutableArray array];
//modify data to be stored in an array
for (int i = 0; i < numPeople; i++) {
AddressBookPerson *person = [[AddressBookPerson alloc] init];
ABRecordRef ref = CFArrayGetValueAtIndex(allContacts, i);
NSString *firstName = (NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSString *lastName = (NSString *)ABRecordCopyValue(ref, kABPersonLastNameProperty);
ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(ref, kABPersonPhoneProperty);
NSArray* phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
CFRelease(phoneNumberProperty);
NSString *contactFirstLast = [NSString stringWithFormat: @"%@, %@", firstName, lastName];
person.name = contactFirstLast;
person.phoneNumbers = phoneNumbers;
[self.contacts addObject:person];
//self.contacts = [NSArray arrayWithArray:listOfContacts];
[contactFirstLast release];
[person release];
}
I’m not sure why this property is not retaining. I have even attempted tried variations such as:
self.contacts = [[NSMutableArray alloc] init];
it is allocated but any object I place inside doesn’t get retained. I used a different array altogether which is not a property and the objects were retained just fine. But the problem is I need to have a global property to contain the values.
UPDATE:
I found the solution to the problem. I ran analyze and did ref counting as well, I got it to the point where there are no memory leaks. But the issue remained and it was because of the name of the property.
“contacts” is a built in property for “The set of MIDI network hosts available for initiating connections and for managing incoming connection requests.”
I named my property a unique name and all the problems disappeared. There is no need for a static variable and I understand that, I was using that variable for testing to see what was going on with my objects. The reason the static variable worked was because it was uniquely named.
The short answer: NSMutableArray is retaining the objects, your program’s implementation has errors in its reference counting.
Please remove all compiler warnings and then run the static analyzer and fix those issues.
(the static analyzer should point out the reference count errors in your program)
Update
The static analysis shows:
This comes to one reference count issue per 4 lines of code, and there are tools dying to help you =)
You really can’t write a nontrivial program until you can do ref counting correctly. You should know it so well, that it is automatic.
Those 5 issues I pointed out may not solve the first issue that you encounter during execution, but fixing each and every one of these issues in your program will remove many active bugs in your program (they are waiting to be discovered).