Reputation:
I'm having a problem in my application with an EXC_BAD_ACCESS after receiving a memory warning. This is how I'm testing: I wrote a simple application that just allocates memory but doesn't free it. After I allocated a lot of memory leaving only about 14 MB free memory, I switch to my main application. Immediately I receive a memory warning in didReceiveMemoryWarning. Moments later my app crashes in a function that sets an image in an UIButton:
-(void)activateRecordButton
{
UIImage *image = [UIImage imageNamed:@"audioRecordOn"];
[recButton setImage:image forState:UIControlStateNormal];
}
The error I get is EXC_BAD_ACCESS(code=1, address=some_address) on the line which calls setImage. If I comment these lines, my app crashes in an another function that sets an image for a UIButton.
This is my didReceiveMemoryWarning:
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
Why is this happening? Is this because there is no more memory to load the images or because recButton was deallocated somehow, when the other app allocated a lot of memory?
I'm running on an Iphone 4 ios 5.1.1
Upvotes: 2
Views: 534
Reputation: 15228
A UIViewController will by default release its view in didReceiveMemoryWarning
(if possible). Your button will also be released if you didn't retain it manually.
setImage
is then called on a nonexistent object and this will trigger a EXC_BAD_ACCESS
.
Upvotes: 3