什么是为UIViewController发布@property / IBOulet的正确方法

时间:2011-05-24 11:36:44

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

我已经阅读了很多关于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;

method.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

1 个答案:

答案 0 :(得分:0)

在dealloc中你释放了aLabel.Means它不在内存中。再次你写这行--- aLabel = nil;删除这一行。所以它不能给Exec_badaccess.This意味着尽管你不没有指针,你试图访问指针。