NSMutableArray访问问题

时间:2010-04-09 05:17:39

标签: objective-c arrays

我搜索过但没有回答。我创建了一个NSMutableArray,并在一个访问位置收到EXC_BAD_ACCESS错误。 这里。这是在.h文件中声明:

NSMutableArray *buttons;
...
@property (nonatomic, retain)NSMutableArray *buttons;

这是合成和恭维:

@synthesize buttons;
...
- (id)init {
    self = [super init];
    if(self != nil) {
        buttons = [[NSMutableArray alloc] init];
    }
    return self;
}
...
-(void)addButtonWithImage:(Image*)image {
    Image *button = image;
    [buttons addObject:button];
    [button release];
}
...
-(void)replaceButtonAt:(int)num with:(Image*)image {
    Image *button = image;
    [buttons replaceObjectAtIndex:num withObject:button];  <<===EXC_BAD_ACCESS
    [button release];
}

但是当我使用它时:

-(void)renderButton:(int)num atPoint:(CGPoint)point center:(BOOL)center{
    Image *button = [buttons objectAtIndex:num];
    [button renderAtPoint:point centerOfImage:center];
}

它有效

1 个答案:

答案 0 :(得分:4)

因为你永远不会分配,保留,复制等等button你不应该发布它

摆脱[button release] s

如果您需要有关引用计数的更多信息,Memory Management Programming Guide for Cocoa是一个有用的读物​​。