5

My app moves 10 UIImageViews randomly around the screen, and once an UIImageView hits the corner, it changes its image. The problem is: after switching between apps and going back to mine, the app crashes.

The console gives me this message:

"App" exited abnormally with signal 10: Bus error

The crash log states this:

Exception Type:  EXC_BAD_ACCESS (SIGBUS)

Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000000000011

Crashed Thread:  0  Dispatch queue: com.apple.main-thread

I think the problem is due to the fact that I'm using UIImage imageNamed, here is the code:

In ViewController.h:

UIImage *red;
UIImage *green;
UIImage *blue;

UIImageView *ballOne;
UIImageView *ballTwo;
UIImageView *ballThree;
UIImageView *ballFour;
// And declare UIImageView for other balls
int clr

In ViewController.m:

- (void)viewDidLoad {
  ...
  red = [UIImage imageNamed: @"redTexture.png"];
  green = [UIImage imageNamed: @"greenTexture.png"];
  blue = [UIImage imageNamed: @"blueTexture.png"];
  ...
}
- (void)moveAll:(NSTimer *)theTimer{
  ...
  // If UIImageView touches a corner, Do this:
  clr = arc4random()%3 + 1;
  switch (clr) {
    case 1:
     [ballOne setImage:red];
     break;
    case 2:
     [ballOne setImage:green];
     break;
    case 3:
     [ballOne setImage:blue];
     break;
    default:
     break;
   }
   // And do this for the rest of 9 "balls" 
}

Why does my App crash, and how do I solve it?

peterh
  • 11,875
  • 18
  • 85
  • 108
NoobDev4iPhone
  • 5,531
  • 10
  • 33
  • 33

2 Answers2

4

[UIImage imageNamed:] returns an autoreleased instance of a UIImage. That means that the memory will be released as soon as the event loop ends.

Yo need to retain those instance either by calling.

[[UIImage imageNamed:@"blabl.png"] retain]

or (preferred method) by setting up your blue, red, green members as property with

@property(nonatomic, retain) UIImage* red;

and your code will be like :

- (void)viewDidLoad {
  ...
  self.red = [UIImage imageNamed: @"redTexture.png"];
  self.green = [UIImage imageNamed: @"greenTexture.png"];
  self.blue = [UIImage imageNamed: @"blueTexture.png"];
  ...
}

Of course do not forget to release them when your done otherwize you will have the opposite of what you have now : memory leaks.

to release red, call

[red release]

inthe dealloc method.

VdesmedT
  • 9,037
  • 3
  • 34
  • 50
  • I'm glad, don't forget to mark the question as "answered" then. (Gray check mark on the left of the answer) – VdesmedT Dec 19 '10 at 11:50
  • One little thing: when I did this without using 'self', nothing changed, but with 'self' your method worked. Can you explain why this makes such a difference? – NoobDev4iPhone Dec 19 '10 at 11:51
  • 1
    It's only by calling self. that you refer to the property and its retain mechanism otherwise, you just direct access the member and don't benefit from the property features. Read apple doc, I specially recommend the memory management article that will avoid hours of debug if well assimilate. Cheers – VdesmedT Dec 19 '10 at 11:53
1

A bus error means that you're attempting to access memory that the CPU physically cannot access. You probably have a stray pointer.

Maybe try the memory allocation debugger?

Pripyat
  • 2,937
  • 2
  • 35
  • 69
  • Hey David, thanks for your reply. This is my 3rd day developing for iPhone. Can you suggest how to avoid this from looking at my code? – NoobDev4iPhone Dec 19 '10 at 11:33