我有一個奇怪的問題,但我很確定,對你來說不是這樣。如果是這樣,請幫我解釋一下。 我有一個39 UIImages數組將用於動畫UIImageView。並在播放動畫後,內存不會釋放。奇怪的釋放問題
- (void)viewDidLoad {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSMutableArray* actionArray = [[NSMutableArray alloc]initWithCapacity:38];
for (int i=1; i<=39; i++) {
NSString* path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"bg_Level1_%.4d", i] ofType:@"png"];
UIImage *image = [[UIImage alloc]initWithContentsOfFile:path];
[path release];
[actionArray addObject:image];
[image release];
NSLog(@"path rc %d image %d", [path retainCount], [image retainCount]);
}
imageView.animationImages = actionArray;
imageView.animationDuration = 4.0;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
NSLog(@"RetainCount ActArr %d ImageView %d", [actionArray retainCount], [imageView retainCount]);
[actionArray release];
[pool drain];
[imageView release];
[self performSelector:@selector(playAnimation2) withObject:self afterDelay:5.0];
[super viewDidLoad];
}
-(void)playAnimation2{
if ([imageView isAnimating]) {
[imageView stopAnimating];
}
NSLog(@"RetainCount ImageView %d",[imageView retainCount]);
}
沒有發現泄漏,但分配的內存量仍然是24 MB。
或者它不是那麼必要?
編輯:
對不起,我的錯。 由於我的代碼有很多變化,我迷失了它。分析後,我明白了! 非常感謝!
正確的代碼是:
- (void)viewDidLoad {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSMutableArray* actionArray = [[NSMutableArray alloc]initWithCapacity:38];
for (int i=1; i<=39; i++) {
NSString* path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"bg_Level1_%.4d", i] ofType:@"png"];
UIImage *image = [[UIImage alloc]initWithContentsOfFile:path];
// [path release];
[actionArray addObject:image];
[image release];
NSLog(@"path rc %d image %d", [path retainCount], [image retainCount]);
}
imageView.animationImages = actionArray;
imageView.animationDuration = 4.0;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
NSLog(@"RetainCount ActArr %d ImageView %d", [actionArray retainCount], [imageView retainCount]);
[actionArray release];
[pool drain];
[self performSelector:@selector(playAnimation2) withObject:self afterDelay:5.0];
[super viewDidLoad];
}
-(void)playAnimation2{
if ([imageView isAnimating]) {
[imageView stopAnimating];
}
[imageView removeFromSuperview];
imageView.animationImages = nil;
NSLog(@"RetainCount ImageView %d",[imageView retainCount]);
}
的問題是與[path release];
-1因爲您的代碼再一次根本不工作。它會立即崩潰,因爲您正在釋放* path *。顯示實際的代碼。編輯:好吧,它不會直接崩潰,但它會*圖像發佈時崩潰。 – Eiko 2010-11-14 17:59:08
我怎樣才能給你整個項目?最有趣的是,沒有任何崩潰。 – 0xDE4E15B 2010-11-14 18:04:47
當然,它不會崩潰,因爲你的圖像不會釋放 - 因此你的問題。 – Eiko 2010-11-14 18:05:29