0xDE4E15B
0xDE4E15B

Reputation: 1294

the proper way to release UIImageView

after asking question about Instruments and finding problems with my code there are two questions:

1st: Why the code above shows in Allocation Instrument exactly what I need?(The memory allocated is less than 30 Mb)

NSMutableArray* arrayOfImages = [[NSMutableArray alloc]initWithCapacity:30];
for (int i = 0; i <= 30; i++){
    NSString* path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"picture%d",i] ofType:@"png"];
    UIImage* img = [[UIImage alloc]initWithContentsOfFile:path];
    [arrayOfImages addObject:img];
    [img release];
}
imageView.animationImages = arrayOfImages;
[arrayOfImages release];
....
imageView.animationImages = nil;
[imageView release];

But on the fact, there are more memory allocated.

According to the developer documentation(Memory Management) I suppose I do everything correct, but on the device program usually crashesh due to the lack of memory. Of course, this is not the full code, but the problem is definetely here.(in application there are 5 UIImageViews with animations and all of them use too much memory.)

EDIT Okay, is it somehow possible to resolve such problem. With memory problems using UIImages?

Upvotes: 0

Views: 401

Answers (3)

user207616
user207616

Reputation:

you can try and wrap a NSAutoreleasePool in your for {} function. I had the same memory problem by creating a huge 86000 size object array. I don't know why it sometimes don't release as it should.

Upvotes: 1

Mark
Mark

Reputation: 6128

You are creating 31 NSImages. If picture0.png or picture30.png do not exist in your bundle you may have a problem.

Try it with i+=5 or i+=10 to see if that fixes the problem.

Upvotes: 0

Eiko
Eiko

Reputation: 25632

Memory management seems ok here.

Are you sure you have images from 0 ... 30, that is 31 in total and not 30 as your code suggests in the array allocation.

As for size, 31 images will take 320 * 480 * 4 * 31 = 19046400 bytes, which requires roughly 18 MB minimum.

Edit: if you have 5 of those, we are probably talking 80+ MB!

Upvotes: 1

Related Questions