我正在處理一個NSNumbers數組,我有一種方法可以根據按鈕來填充它們;Malloc錯誤:在OBJC中雙倍空閒
- (NSMutableArray *)addNumberToArray:(int)numPressed andWithGuessArray:(NSMutableArray *)guessArray andWithActiveIndex:(int)activeIndex {
NSNumber *numberPressed = [NSNumber numberWithInt:numPressed];
switch (numPressed) {
case 1:
[guessArray replaceObjectAtIndex:activeIndex withObject:numberPressed];
break;
case 2:
[guessArray replaceObjectAtIndex:activeIndex withObject:numberPressed];
break;
case 3:
[guessArray replaceObjectAtIndex:activeIndex withObject:numberPressed];
break;
case 4:
[guessArray replaceObjectAtIndex:activeIndex withObject:numberPressed];
break;
case 5:
[guessArray replaceObjectAtIndex:activeIndex withObject:numberPressed];
break;
case 6:
[guessArray replaceObjectAtIndex:activeIndex withObject:numberPressed];
break;
case 7:
[guessArray replaceObjectAtIndex:activeIndex withObject:numberPressed];
break;
case 8:
[guessArray replaceObjectAtIndex:activeIndex withObject:numberPressed];
break;
case 9:
[guessArray replaceObjectAtIndex:activeIndex withObject:numberPressed];
break;
default:
break;
}
[numberPressed release];
for (int i = 0; i < 4; i++) {
NSLog(@"%i",[[guessArray objectAtIndex:i]intValue]);
}
NSLog(@"----");
return guessArray;
}
然後,我必須刪除該陣列中的所有數字的方法。
- (void)deleteAll:(id)sender {
NSLog(@"%@",guessArray);
for (int i = 0; i < numbersInAns; i++) {
[guessArray replaceObjectAtIndex:i withObject:[NSNumber numberWithInt:0]];
}
activeIndex = 0;
for (int i = 0; i < 4; i++) {
NSLog(@"%i",[[guessArray objectAtIndex:i]intValue]);
}
}
的問題時,當我填充數組,清除它,再填寫一次,並再次嘗試清除它。
2011-03-19 13:58:43.492 BombDefusal [4711:207]( 2, 2, 3,)//這是什麼其清零前右陣列中,這些都是NSNumbers
BombDefusal(4711,0xa096a540)的malloc:*錯誤對象0x4e0cc40:雙免費 *設置malloc_error_break斷點調試
(GDB)打印對象0x4e0cc40
解決
這可能是一個愚蠢的問題,但爲什麼你使用開關而不是單個if語句? – jonmorgan 2011-03-19 18:16:00
哈哈它曾經是一個必要的開關,但我改變了它,但我沒有注意到它現在是額外的代碼 – Raplesyrup1 2011-03-19 19:01:15