使用弧的内存问题

时间:2012-09-29 16:47:26

标签: objective-c memory-management memory-leaks automatic-ref-counting uiviewanimation

我是Objective-C编程的新手,我遇到了典型的内存问题。我必须做一个基于导航控制器的应用程序,当传递几个视图(使用推视图控制器)时,加载100个图像的动画。在模拟器中运行良好,但在手机上没有...我打开不同的动画,然后它关闭。我正在使用弧来避免这种情况,但似乎没有工作。我也尝试禁用arc并手动释放UIImageView但它甚至很快就崩溃了。以下是其中一个观点的示例:

    //.h
@interface Gegant_nou : UIViewController {

IBOutlet UIImageView *ImageViewGegant; 
}

@property (nonatomic, strong) UIImageView* ImageViewGegant;

//.m

- (void)viewDidLoad {

    [super viewDidLoad];


    UIBarButtonItem *rigthButton = [[UIBarButtonItem alloc] initWithTitle:@"Detalls" style:UIBarButtonItemStyleBordered target:self action:@selector(canviarDetalls)];
    self.navigationItem.rightBarButtonItem = rigthButton;
    [rigthButton release];


    ImageViewGegant.animationImages =@
        [[UIImage imageNamed:@"0001.png"],
        [UIImage imageNamed:@"0002.png"],
        . load all the images
        .
        [UIImage imageNamed:@"0099.png"],
        [UIImage imageNamed:@"0100.png"]];

    ImageViewGegant.animationDuration = 4;
    ImageViewGegant.animationRepeatCount = 0;
    [ImageViewGegant startAnimating];
    [self.view addSubview:ImageViewGegant];

    self.title = @"Gegant nou";

    [ImageViewGegant release];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.

}

- (void)viewDidUnload{
    [super viewDidUnload];
    [ImageViewGegant release];
}

知道为什么会这样吗?谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

如果您提供了一些崩溃日志或堆栈跟踪来提供有关问题所在位置的更多信息,那么帮助您会更容易。

你完全确定问题与记忆有关吗?由于您使用的是ARC,因此除非您的代码库中有部分不使用ARC,或者您使用的是CoreGraphics等c库,即使使用ARC仍然需要保留/释放,否则不太可能。

如果你有过度释放的内存问题并且你得到EXC_BAD_ACCESS崩溃,你可以尝试按产品启用应用中的僵尸 - >编辑方案.. - >诊断然后“启用Zombie对象”。这有望提供导致问题的对象的更多信息。

在一个完全不同的主题上,我强烈建议您使用camelcase编写所有实例变量。阅读令人困惑,大多数开发人员都会认为“ImageViewGegant”是一个类的名称。

答案 1 :(得分:0)

问题解决了!发生的事情是我使用UIImageNamed来加载动画,虽然我使用的是ARC,但UIImageNamed会加载图片但不会释放它,因此它会加载所有帧并快速填充内存。现在我没有那些记忆问题。谢谢大家!