奇怪的释放问题

0xD*_*15B -1 iphone memory-management objective-c

我有一个奇怪的问题,但我很确定,因为你不是.如果是这样,请帮助我或解释.我有一个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]);
}
Run Code Online (Sandbox Code Playgroud)

没有发现泄漏,但分配的内存量仍为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]);
}
Run Code Online (Sandbox Code Playgroud)

问题在于 [path release];

bbu*_*bum 6

这段代码有很多问题.它完全是一个奇迹.

第一:

不要使用-retainCount.

对象的绝对保留计数是没有意义的.

您应该调用release与保留对象完全相同的次数.不会少(除非你喜欢泄漏),当然,没有更多(除非你喜欢崩溃).

有关完整详细信息,请参阅内存管理指南


现在,一些问题:

  • 你问是否需要清空数组,然后说array = nil; ......不起作用.将数组引用设置为nil对数组没有任何作用; 不释放它或清空它.

  • 你过度释放了 path

  • 你正在加载一系列图像,将它们粘贴在一个数组中,并且该代码中没有任何东西可以释放数组(或清空它).你的应用程序继续使用内存,并显示没有泄漏听起来像正确的行为.

  • 你为什么要发布imageView?该代码中没有任何内容暗示您保留它,即使它是通过其他机制(IB?)保留的,那么释放它也是一个非常奇怪的地方.