Getting EXC_BAD_ACCESS Error when setting an Image on a UIButton

Discussion in 'iOS Programming' started by 292, Mar 22, 2009.

  1. 292 macrumors newbie

    Mar 22, 2009
    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:

    #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;
    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";
  2. eddietr macrumors 6502a

    Oct 29, 2006
    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.
  3. bboyjayz macrumors member

    Mar 20, 2009
    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 ...
  4. buckyballs macrumors regular

    Dec 22, 2006
    You also have this problem if you use NSArrays objectAtIndex method and try to access and index that doesn't exist.
  5. 292 thread starter macrumors newbie

    Mar 22, 2009
    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...

Share This Page