2010-08-29 93 views
0

時,我有以下代碼:獲得EXC_BAD_ACCESS試圖ADDOBJECT到NSMutableArray的

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]); 

任何想法,爲什麼我會得到EXC_BAD_ACCESS,當我嘗試做[phoneKeys ADDOBJECT:phoneIndex]?

在此先感謝

+0

您是否嘗試過刪除NSLog行? – kennytm 2010-08-29 18:13:36

+0

嗯你是對的。這是否意味着phoneKeys正在發佈太快? – john 2010-08-29 18:16:54

+0

你的意思是它在刪除NSLog後工作? – kennytm 2010-08-29 18:20:34

回答

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

-count方法返回一個NSUInteger,它只是一個unsigned int。但是%@只能打印Objective-C對象,而不是unsigned int。這導致異常。

要打印unsigned int,您需要使用%u而不是%@

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

謝謝!這幫助很大 – john 2010-08-29 18:31:22

0

每當這個發生在我身上,即它打破了正確的內存管理代碼,並根據內存管理準則,但劇照崩潰,其結果是由於過度釋放somwehere其他,我會做的代碼進行全面審查尋找泄漏,我推薦你使用泄漏工具:

在X-代碼去運行>與性能工具運行「泄漏

入住此技術問答&一太:http://developer.apple.com/mac/library/qa/qa2004/qa1367.html

相關問題