Iphone Malloc错误:OBJC中的双自由

Iphone Malloc错误:OBJC中的双自由,iphone,Iphone,我正在处理一个NSNUMBER数组,我有一种方法可以根据按下的按钮填充它们 - (NSMutableArray *)addNumberToArray:(int)numPressed andWithGuessArray:(NSMutableArray *)guessArray andWithActiveIndex:(int)activeIndex { NSNumber *numberPressed = [NSNumber numberWithInt:numPressed]; switch (nu

我正在处理一个NSNUMBER数组,我有一种方法可以根据按下的按钮填充它们

 - (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;
-(NSMutableArray*)addNumberToArray:(int)numPressed and WithGuessArray:(NSMutableArray*)guessArray and WithActiveIndex:(int)activeIndex{
NSNumber*numberPressed=[NSNumber numberwhithint:numPressed];
开关(numPressed){
案例1:
[guessArray replaceObjectAtIndex:activeIndex with Object:numberPressed];
打破
案例2:
[guessArray replaceObjectAtIndex:activeIndex with Object:numberPressed];
打破
案例3:
[guessArray replaceObjectAtIndex:activeIndex with Object:numberPressed];
打破
案例4:
[guessArray replaceObjectAtIndex:activeIndex with Object:numberPressed];
打破
案例5:
[guessArray replaceObjectAtIndex:activeIndex with Object:numberPressed];
打破
案例6:
[guessArray replaceObjectAtIndex:activeIndex with Object:numberPressed];
打破
案例7:
[guessArray replaceObjectAtIndex:activeIndex with Object:numberPressed];
打破
案例8:
[guessArray replaceObjectAtIndex:activeIndex with Object:numberPressed];
打破
案例9:
[guessArray replaceObjectAtIndex:activeIndex with Object:numberPressed];
打破
违约:
打破
}
[预计发布数量];
对于(int i=0;i<4;i++){
NSLog(@“%i”,[[guessArray objectAtIndex:i]intValue]);
}
NSLog(@“-”);
返回数组;
}

然后我有一个方法删除数组中的所有数字

- (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]);
}
-(void)deleteAll:(id)发送方{
NSLog(@“%@”,数组);
对于(int i=0;i
}

当我填充数组,清除它,再次填充,然后再次尝试清除它时,就会出现问题

2011-03-19 13:58:43.492炸弹防御[4711:207]( 2. 2. 3. 3. )//这是数组中清除之前的内容,这些都是NSN编号

BombDefusal(4711,0xa096a540)malloc:*对象0x4e0cc40的错误:双自由 *在malloc\u error\u break中设置断点以进行调试

(gdb)打印对象0x4e0cc40

二,


已解决

您收到的错误消息显示“在malloc\u error\u break中设置断点以进行调试”。你试过了吗

您正在对
numberPressed
对象调用
release
,但您从未
保留它。
NSNumber
类方法
numberwhithint:
将自动释放的对象返回给您。
release
调用是不必要和不正确的

除此之外,switch语句中的每个条目都包含完全相同的代码-为什么要编写switch语句?您可以将整个块替换为一行:

[guessArray replaceObjectAtIndex:activeIndex withObject:numberPressed];

这可能是一个愚蠢的问题,但是为什么你要使用一个开关而不是一个if语句呢?哈哈,它曾经是一个必要的开关,但我改变了它,但我没有注意到它现在是额外的代码哦,哈哈,是的,它曾经有一些不同的代码需要开关,但我做得更好,我没有注意到我不需要开关