【问题标题】:Memory problems using arc使用 arc 的内存问题
【发布时间】:2012-09-29 16:47:26
【问题描述】:

我是 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];
}

知道为什么会发生吗?谢谢你帮助我!

【问题讨论】:

  • 1.哪一行抛出异常? 2. -viewDidUnload 在 iOS6 下被弃用,你应该避免使用它(它永远不会被调用)。 3. 为什么不使用循环初始化图像数组?
  • 关于使用 ImageNamed 加载与 initwtihcontentsofffile 的内存使用存在一些争论。有关其他选项stackoverflow.com/questions/6566827/… 的一些有用信息,请参阅此帖子
  • @holex 我应该用什么来代替 viewDidUnload?解除分配?
  • @user1600400,根据文档,您应该使用-didReceiveMemoryWarning 方法来释放您不再需要的所有内容。
  • 好的@holex,我会的,谢谢!

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


【解决方案1】:

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

您完全确定问题与内存有关吗?由于您使用的是 ARC,除非您有部分代码库不使用 ARC,或者如果您使用 CoreGraphics 等 c 库,即使您使用 ARC,您仍然需要保留/释放。

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

在一个完全不同的主题上,我强烈建议您使用驼峰写法编写所有实例变量。读起来很混乱,大多数开发人员会认为“ImageViewGegant”是一个类的名称。

【讨论】:

  • 我有内存问题,正如你所说,我得到 EXC_BAD_ACCESS view the capture here。我正在使用 coregraphics 库。谢谢大家。
【解决方案2】:

问题解决了!发生的事情是我使用 UIImageNamed 加载动画,虽然我使用的是 ARC,但 UIImageNamed 加载图像但不释放它,所以它加载所有帧并很快填满内存。现在我没有那个记忆问题了。谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多