views:

38

answers:

1

I've searched and have no answer. I've created an NSMutableArray and am getting an EXC_BAD_ACCESS error in one place of access. Here. This is declaring in the .h file:

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

And this is the synthesizing and implimenting:

@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];
}

But when I use this:

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

It works

+3  A: 

since you never allocate, retain, copy, etc. button you should not be releasing it

just get rid of the [button release]s

The Memory Management Programming Guide for Cocoa is a useful read if you need more info on reference counting.

cobbal
Thanks, that worked, but I thought since I had stored the object in the array that it would have a retain count of 2 and I had to bring it down one. Or am I only storing a pointer to the object?
Danegraphics
You should never worry about what the retain count is. What you should do is make sure to release anything you own. Since you don't alloc, retain or copy it, you don't own it. `buttons` will retain it, but it will then be the responsibility of `buttons` to release it. (It is your responsibility to release `buttons`, however, as you `alloc` ed it)
cobbal