【问题标题】:Whats the correct way to release a @property / IBOulet for UIViewController为 UIViewController 释放 @property / IBOulet 的正确方法是什么
【发布时间】:2011-08-31 20:56:54
【问题描述】:

我已经阅读了许多关于 cocoa/objective-c 正确内存管理的不同内容

例如,我读到任何 IBOutlets 都需要设置为“nil”,但类似于 NSArray dosnt?

我也想知道,在我释放/取消所有内容之前或之后调用超级方法是否重要

为了解决这个内存问题,请用 100% 正确的方式回复您将创建保留属性并释放它的方法。如果您不是 100% 确定,请不要回答。

这是我目前正在做的事情,但显然有些问题,因为我得到了非常令人沮丧的 EXEC_BAD_ACCESS!?!几乎就像我发布了两次一样?

header.h

@interface MyViewController : UIViewController {

    UILabel *aLabel;
    NSArray *aArray;

}
@property (nonatomic, retain) IBOutlet UILabel *aLabel;
@property (nonatomic, retain) NSArray *aArray;

方法.m

@implementation MyViewController

@synthesize aLabel, aArray;

- (void)dealloc
{
     [aLabel release], aLabel = nil;
     [aArray release];
     [super dealloc];
}

- (void)viewDidUnload
{
    self.aLabel = nil; //Not sure about this bad boy???
    [super viewDidUnload];
}

@end

【问题讨论】:

  • 你的想法是对的。 dealloc 中的 'aLabel = nil' 是不必要的,但无害。你的崩溃发生在哪里?
  • 天哪,我刚刚发现我出于某种疯狂的原因以我的一种方法释放了我的财产。我知道它发布了两次!那么我在做什么是正确的呢?只需在 dealloc 中省略 aLabel = nil 以及所有快乐的日子吗?
  • 'aLabel = nil' 不会崩溃或任何事情。它只是清除了一个即将与其余对象一起消失的 ivar。所以没有必要。

标签: objective-c cocoa-touch xcode memory-management memory-leaks


【解决方案1】:

在 dealloc 中,你已经释放了 aLabel。意味着它不在内存中。再次你写这一行 ---aLabel=nil;删除这一行。这样它就不能给 Exec_badaccess。这意味着即使你不'没有指针仍然试图访问指针。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 2011-08-29
    • 2011-05-07
    • 1970-01-01
    • 2011-12-13
    • 2011-10-08
    • 2013-08-05
    相关资源
    最近更新 更多