views:

21

answers:

2

My app crashes due to memory building up after every animation.

Here is my sample code:

-(IBAction) shoot: (id)delegate{
[gun setImage:[UIImage imageNamed:@"animation_machin_guns_320x480_7.png"]];
UIImage *frame1 = [UIImage imageNamed:@"animation_machin_guns_320x480_1.png"];
UIImage *frame2 = [UIImage imageNamed:@"animation_machin_guns_320x480_2.png"];
UIImage *frame3 = [UIImage imageNamed:@"animation_machin_guns_320x480_3.png"];
UIImage *frame4 = [UIImage imageNamed:@"animation_machin_guns_320x480_4.png"];
UIImage *frame5 = [UIImage imageNamed:@"animation_machin_guns_320x480_5.png"];
UIImage *frame6 = [UIImage imageNamed:@"animation_machin_guns_320x480_6.png"];
UIImage *frame7 = [UIImage imageNamed:@"animation_machin_guns_320x480_7.png"];    
gun.animationImages = [[NSArray alloc] initWithObjects:frame1, frame2, frame3, frame4, frame5, frame6, frame7,nil];
gun.animationDuration = 1;
gun.animationRepeatCount = 1; 
[gun startAnimating];
[frame1 release];
[frame2 release];
[frame3 release];
[frame4 release];
[frame5 release];
[frame6 release];
[frame7 release];}

Releasing the frames doesn't seem to do the magic. I tried using this http://kosmaczewski.net/projects/iphone-image-cache/ for image caching but i guess I don't know how to use it properly since the memory builds up faster than using imageNamed.

+1  A: 

Replace [[NSArray alloc] initWithObjects:xxx]; with [NSArray arrayWithObjects:xxx];.

Currently, the array object is being retained twice (once effectively by alloc and once by gun.animationImages = xxx), but only released once (when the gun object is released, or the animationImages property is set to something new), meaning it will never be released.

The arrayWithObjects method returns an autoreleased object, meaning it doesn't need to be manually released by you.

grahamparks
+1  A: 

If I were you I would think about only animating parts of the screen or perhaps compressing the images into low quality jpgs or something along those lines.

vakio
I need all the screen to be animated and I've already minimized the file size for the images to as low as possible.
Subject78