为 UIViewController 发布 @property / IBOulet 的正确方法是什么
我读过许多关于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 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.