views:

89

answers:

2

I have the following code:

NSInteger phoneCount = ABMultiValueGetCount(phones);
NSMutableArray *phoneKeys = [[[NSMutableArray alloc] init] autorelease];
 NSMutableArray *phoneKeyValues = [[[NSMutableArray alloc] init] autorelease];

 for(CFIndex i=0; i < phoneCount; i++) {
  //NSString *label = [(NSString *)ABMultiValueCopyLabelAtIndex(phones, i) autorelease];
  NSString *phone = [(NSString *)ABMultiValueCopyValueAtIndex(phones, i) autorelease];
  NSString *phoneIndex = [[[NSNumber alloc] initWithInt:ABMultiValueGetIdentifierAtIndex (phones, i)] autorelease];
  [phoneKeys addObject:phoneIndex]; // it breaks on this line
 }

NSLog(@"Count: %@ %@", [phoneKeys count], [phoneKeyValues count]);

Any idea why I would get EXC_BAD_ACCESS when I try to do [phoneKeys addObject:phoneIndex]?

Thanks in advance

A: 

Whenever this has happened to me, i.e. the code where it breaks has proper memory management, and according to the memory management guidelines but stills crashes, it results to be due to over releasing somwehere else, I would do a complete review of the code looking for leaks and I recommend you to use the leaks tool:

In X-Code go Run > Run with performance tool > Leaks

Check this Technical Q&A too: http://developer.apple.com/mac/library/qa/qa2004/qa1367.html

Leg10n
+1  A: 
NSLog(@"Count: %@ %@", [phoneKeys count], [phoneKeyValues count]);

The -count method returns an NSUInteger, which is just an unsigned int. But %@ can only print Objective-C objects, not unsigned int. This causes the exception.

To print unsigned int, you need to use %u instead of %@.

NSLog(@"Count: %u %u", [phoneKeys count], [phoneKeyValues count]);
KennyTM
Thanks!!!! This help s alot
john