I've been coding for a while now in objective-c and am comfortable with it... but one thing eludes me. Memory management. I'm releasing as I think is correct, but this bit of code is throwing a "EXC_BAD_ACCESS" and crashes the app.
When I comment out and DON'T release the button and image, it works fine. The function is called to read through an array of image filenames.
for (x=items_start;x<items_stop;x++) {
UIButton *button;
UIImage *buttonImage;
buttonImage = [UIImage imageNamed:[NSString stringWithFormat:@"%i.png", x]];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = x;
[button setImage:buttonImage forState:UIControlStateNormal];
[button addTarget:self action:@selector(duplicateImage:) forControlEvents:UIControlEventTouchUpInside];
[viewBasicItems addSubview:button];
[buttonImage release];
[button release];
}
any ideas? Like i said, when I comment out the last two lines (releasing the button and image) it works OK. Is this normal or should I be able to release them?
Note: I have remove a fair bit of other code to show this example in a smaller chunk!