PDA

View Full Version : Getting EXC_BAD_ACCESS Error when setting an Image on a UIButton




292
Mar 22, 2009, 09:38 PM
I'm getting the dreaded EXC_BAD_ACCESS error when I'm trying to set an
image on a UIButton.

I read the Apple doc on memory management and still can't figure out
what I'm doing wrong. I've verified my images are all accessible and working in my Resources folder.

I've narrowed it down to the exact line of code that's causing the
error. If you can help me figure out what I'm doing wrong and how to fix
it, I'll be eternally grateful

I have a very simplified version of my code:
Here's my Header File:

MyController.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface MyController : UIViewController {
IBOutlet UIButton *button1;
}

@property (nonatomic, retain) UIButton *button1;

- (IBAction)clickedAButton:(id)sender;
- (NSString *)doSomething:(int)someNumber;

@end


And here's my implementation file. I noted in the comments below which line causes the error:

#import "MyController.h"

@implementation MyController
@synthesize button1;

- (IBAction)clickedAButton:(id)sender{
NSString *myString;
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//This line works perfectly
[sender setImage:[UIImage imageNamed:@"Red.png"] forState:UIControlStateNormal]; //This line works Perfectly

myString = [self doSomething:1];
}

- (NSString *)doSomething:(int)someNumber {

[button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];//This line works perfectly
[button1 setImage:[UIImage imageNamed:@"Blue.png"] forState:UIControlStateNormal];//This line causes the EXC_BAD_ACCESS Error

return @"didSomething";
}



eddietr
Mar 22, 2009, 11:13 PM
I don't see anything wrong with what you've posted. I assume you must be doing something else interesting between setting the color and background to one thing and then setting it back to another.

My guess is some of the code you didn't post is actually where the bug is.

bboyjayz
Mar 23, 2009, 07:10 PM
that's true, i don't see mistake on this code me too ... i already had this error, that's was a problem of cache when i changed subviews of the windows.... mayby it will help you ...

buckyballs
Mar 24, 2009, 06:17 AM
You also have this problem if you use NSArrays objectAtIndex method and try to access and index that doesn't exist.

292
Mar 24, 2009, 10:21 PM
Thank you everybody for your responses.

eddietr, you are correct. I cut out all the extraneous code to focus on the bare minimum amount of code to focus on the problem. When I ran this code, the app worked. I'm going to paste back in the rest of the code line by line until I figure out which line is causing the issue.

Thank you all again... I may be back...