views:

161

answers:

1

Hi, In a very small number of cases in my iphone app, I get a crash after the for loop in the code below:

ABAddressBookRef addressBookInit = ABAddressBookCreate();

CFMutableArrayRef abContacts =
(CFMutableArrayRef)ABAddressBookCopyArrayOfAllPeople(addressBookInit); // get array of all contacts
CFArraySortValues (abContacts, CFRangeMake(0, CFArrayGetCount(abContacts)), (CFComparatorFunction)ABPersonComparePeopleByName, (void *)ABPersonGetSortOrdering());

NSArray *copypeople = (NSArray *) abContacts;
NSMutableArray *tempTheadlist = [[NSMutableArray alloc] init];

for (int i=0; i < copypeople.count; i++)
{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   ABRecordRef record = [copypeople objectAtIndex:i];
   if (blah blah)
         [tempThreadList addObject: someObject];
   [pool release];
}

// POINT OF CRASH AFTER LOOP ENDS

if (tempTheadlist.count > 0)
     [NSThread detachNewThreadSelector: @selector(loading_pictures:) toTarget:self withObject:tempTheadlist];


[tempTheadlist release];
[copypeople release];
CFRelease(addressBookInit);

Any reason why it should crash at any point here?

+1  A: 

(1) ABAddressBookCopyArrayOfAllPeople() returns a CFArrayRef, not a CFMutableArrayRef.

(2) All the casts between CFArrayRef, CFMutableArrayRef, and NSArray are irrelevant. NSArray and CFArray are synonymous.

(3) The autorelease pool in the for() loop is irrelevant. There aren't any new objects being created in the for() loop and, thus, nothing to fall in the pool. Nor will objectAtIndex: retain/autorelease the objects.

(4) That you are sorting the array returned by ABAddressBookCopyArrayOfAllPeople() may likely be a crash trigger. That function is declared as returning a CFArrayRef() and it should be treated as immutable.

If the app is crashing, post the backtrace of the crash. Without that, it is hard to tell what is specifically triggering the crash. Is it crashing in the thread that ran the above code? ... or crashing in the newly created thread?

bbum
3 - Unless the creation of "someObject" happens in there.
BJ Homer
Yes, someObject is created. And the crash doesn't happen at the sorting part, but after the for loop. And there is no backtrace ... this is a live issue, but it doesn't generate a crash, just hangs for the user.
z s
You should be able to break into the debugger when it is hung and get a backtrace. However, that it is hanging may be because of (4) -- you may be moving AB's cheese.
bbum