iphone UIImage array memory leak (old)
I posted another question similar. This is my old code as some ppl say it would be even better, but there i didnt found a solution to release the allocated memory.
imageArray_danceright = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"FrankieDanceRetime_0001.jpg"],
.. 40 images
[UIImage imageNamed:@"FrankieDanceRetime_00040.jpg"],nil];
imageArray_danceleft = [[NSArray alloc] initWithObjects:
[UII开发者_C百科mage imageNamed:@"FrankieDanceRetime_0041.jpg"],
.. 40 images
[UIImage imageNamed:@"FrankieDanceRetime_00080.jpg"],nil];
imageArray_stand= [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"FrankieSingtRetime_0001.jpg"],
[UIImage imageNamed:@"FrankieSingtRetime_0002.jpg"],
[UIImage imageNamed:@"FrankieSingtRetime_0003.jpg"],nil];
//start animation
myimageview.animationImages = imageArray_stand;
myimageview.animationDuration = 0.23;
myimageview.contentMode = UIViewContentModeBottomLeft;
myimageview.animationRepeatCount = 0.0;
myimageview.image = [myimageview.animationImages objectAtIndex:1];
[myimageview startAnimating];
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
touch = [[event allTouches] anyObject];
if ([touch view] == overlay_fuesserechts) {
[myimageview setAnimationImages:imageArray_danceright];
myimageview.animationDuration = 2.0;
myimageview.contentMode = UIViewContentModeBottomLeft;
myimageview.animationRepeatCount = 1;
[myimageview startAnimating];
}
if ([touch view] == overlay_fuesselinks) {
[myimageview setAnimationImages:imageArray_danceleft];
myimageview.animationDuration = 2.0;
myimageview.contentMode = UIViewContentModeBottomLeft;
myimageview.animationRepeatCount = 1;
[myimageview startAnimating];
}
Its works fine.. just when i look in Instrument/Allocation, it does not release the memory. It starts with 8mb for the 'stand' anim, goes up to 32mb for fuesserechts and when i click fuesselinks it goes up to 55mb (i have 6 more anim.. so a few more and it just crash) When I do set [myimageview setAnimationImages:nil]; (in touchesBegan) for each setAnimationImages ... i still does not release the Memory. All Allocatoins Overall bytes, still increase. When I do set [myimageview.animationImages release]; in (touchesbegan) for each setAnimationImages... it even crash after a few touches.
Thanks Chris
Ok, now I see your original code I think I see where the advice came from. Instead of imageNamed:
you might want to use imageWithContentsOfFile:
(as you did in your earlier question). The former caches its images whereas the latter does not. I'm not convinced this is your problem here though.
What is less clear is where this code fits into your bigger system. I presume the first stretch of code (all the imageNamed:
calls) is in an init method? Is that the case?
Is the //start animation
section really in the same method, or somewhere else. This is not clear because you then include your touchesBegan:
method in its entirety.
You also don't show where myimageview
is being created - and who owns it. If you create it within the same class then you should also be releasing it in dealloc
. Talking of dealloc
it seems to be missing from this version - was that only in the new version? You need it here (more so).
I know its hard to post snippets of code unambiguously - but in this case I think these are the things that need clarification.
If your setup code (the imageNamed:
stretch) is being called every time that may well explain it - as might the lack of releasing myimageview
.
In this code, it looks like you're possibly initializing three arrays each click but you never release them so the arrays just stack up in memory. Alternatively, you may not be actually retaining the arrays at all. I can't tell because code provided where the arrays are initialized has no context to the rest of the class.
You should make the arrays of images retained properties of the class so that you only initialize them once. Use the self.
notation to access them properly so that they are retained. Release them only in the class' deallocation method.
Don't ever send a release to another object's properties. Each object should exclusively manage its own memory use. In this case sending the UIImageView's animationImages
property release is a big error. It will almost always cause a crash.
As an aside, it's dangerous to initialize the arrays with a large numbers of of imageNamed
calls. If something goes wrong with a particular image, you will have a hard time tracking the error down. Instead, use a mutable array and build the array with a for loop. Doing so will make the code more compact and make errors obvious.
精彩评论