views:

40

answers:

2

I am a beginner at this :)

I have an app which changes an image of a UIImageView when a button is pushed, each image is about 200 kb, when run in the simulator it runs ok, I can change the image many times with no problems.

When run on my device (ipod touch) it loads ok, it sluggishly gets through about 4 images and then it crashes. When I run it with the Allocations performance tool, it crashes when overall bytes reaches about 2.75 megs. Number of living allocations is about 8000 (is this high?).

My console reads this

Program received signal:  “0”.
Data Formatters temporarily unavailable, will re-try after a 'continue'. (Unknown error loading shared library "/Developer/usr/lib/libXcodeDebuggerSupport.dylib")

I've also tried using the "leaks" performance tool and it doesn't find any leaks.

My .h file loads the image and uimageview like this:

@property(retain, nonatomic) IBOutlet UIImageView* myUIImageView;
@property(nonatomic, retain) UIImage *image;

Also, I release these like this:

- (void)dealloc {
[super dealloc];
[myUIImageView release];
[image release];
}

I also added in this, but it didn't seem to make any difference:

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    [myUIImageView release];
    [image release];
    // Release any cached data, images, etc that aren't in use.
}

I'm not sure what else to try at this point, any debugging techniques suggestions would be appreciated, also any pointers on how tackle memory issues like this would be hugely helpful, thanks!

Also, sorry forgot to add the image changing code:

- (IBAction)myButton {  

    static NSUInteger counter = 0;

    counter++;

    if (counter == 1) {
        myUIImageView.image = [UIImage imageNamed:@"image1.jpg"]; 
    }
    else  {
        myUIImageView.image = [UIImage imageNamed:@"image2.jpg"];
            counter = 0;
    }
}
A: 

What code are you using to change the image? Make sure you are not re-alloc'ing the image or imageview.

Ben
Thanks for your response Ben, please see my edit.
Pete Herbert Penito
+2  A: 

Your image changing code looks fine, but I suggest these changes:

You should set myUIImageView to nil in your viewDidUnload method:

- (void)viewDidUnload {
    // release retained subviews of main view
    self.myUIImageView = nil;
}

In didReceiveMemoryWarning, you should set image to nil instead of sending a release:

self.image = nil;

Having [image release] in both didReceiveMemoryWarning and dealloc might cause a double release.

Also, don't release myUIImageView in didReceiveMemoryWarning.

Are you actually using the image ivar? It looks like you are assigning an image directly to the UIImageView. Having an unused ivar doesn't cause a problem, obviously, but I'm just wondering why it's there.

Josh Hinman
thank u for your assistance Josh, I added in my image changing code
Pete Herbert Penito
Modified my answer in response. ;-)
Josh Hinman
Well I'm not quite sure if I'm using the ivar, I synethesize the image at the top of my .m file: @synthesize image; because it was warning me that it didn't exist even though it was compiling, also should I set myUIImageView to nil in didReceiveMemoryWarning as well as the image? or is it ok to release it here.
Pete Herbert Penito
There are three steps to creating a property: declare an ivar, write a @property declaration, and @synthesize it. I suspect you are probably missing step 1 for image, but I also suspect you don't need it. So just get rid of the @property image and the @synthesize image. And you should not do anything to myUIImageView in didReceiveMemoryWarning. You should set it to nil in vewDidUnload (using the self.myUIImageView method), and you should release it in dealloc, but that's it.
Josh Hinman
Awesome man, thank you for your patience, its performance has certainly improved, I can get through like double the images compared to before, I think I may try lowering the file size of the images a little and see if that stops the crashing.
Pete Herbert Penito