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

发布于 2024-11-09 13:12:29 字数 849 浏览 0 评论 0原文

我读过许多关于 cocoa/objective-c 正确内存管理的不同内容,

例如,我读过任何 IBOutlet 都需要设置为“nil”,但像 NSArray 这样的东西不需要设置吗?

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

要解决此内存问题,请用 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

I've read many different things about correct memory management for cocoa/objective-c

For instance ive read that any IBOutlets need to be set to 'nil' but something like an NSArray dosnt?

I would also like to know, is it important to call the super method before or after i release/nil everything

To put this memory issue to bed, can some please reply with the 100% correct way you would create a retained property and release it. If your not 100% sure please dont answer.

Here is what im currently doing but something is obviously wrong as i get the very frustrating EXEC_BAD_ACCESS!?! Almost like im releasing something twice?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生漫 2024-11-16 13:12:29

在 dealloc 中,您已经释放了 aLabel。意味着它不在内存中。您再次编写此行 ---aLabel=nil;删除此行。这样它就不能提供 Exec_badaccess。这意味着即使您没有指针仍然是您正在尝试访问该指针。

In dealloc you have released the aLabel.Means it is not in memory.Again you are write this line ---aLabel=nil;Remove this line.So that it can't gives the Exec_badaccess.This means eventhough you don't have pointer stilll you are trying to access the pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文