开发者

Malloc error: double free in OBJC

I'm working with an array of NSNumbers, and i have a method to fill them based on the button pressed;

 - (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 r开发者_运维技巧eplaceObjectAtIndex: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;

}

Then i have a method to delete all the numbers in the array.

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

}

The problem occurs when i fill the array, clear it, fill it again, and try to clear it again.

2011-03-19 13:58:43.492 BombDefusal[4711:207] ( 2, 2, 3, 3 ) //this is whats in the array right before its cleared, these are all NSNumbers

BombDefusal(4711,0xa096a540) malloc: * error for object 0x4e0cc40: double free * set a breakpoint in malloc_error_break to debug

(gdb) print-object 0x4e0cc40

2

Solved


The error message you're getting says "set a breakpoint in malloc_error_break to debug". Did you try that?

You're calling release on the numberPressed object, but you didn't ever retain it. The NSNumber class method numberWithInt: returns an autoreleased object to you. That release call is unnecessary and incorrect.

Besides that, every entry in your switch statement contains exactly the same code - why did you write the switch statement at all? You could replace that whole block with just the one line:

[guessArray replaceObjectAtIndex:activeIndex withObject:numberPressed];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜