Objective C 自动释放
你好,我不完全理解 obj-C 中的 autorelease 函数调用。
@interface A{
id obj;
}
@implementation A
-(void)myMethod;
{
obj = [BaseObj newObj]; //where newObj is a method like :[[[BaseObj alloc]init]autorelease];
}
-(void)anotherMehtod;
{
[obj someMeth]; //this sometimes gives me EXC_BAD_ACCESS
}
@end
所以为了解决这个问题我放了一个保留。现在如果我保留这个对象,我需要手动释放它吗?
Hello I do not fully understand the autorelease function call in obj-C.
@interface A{
id obj;
}
@implementation A
-(void)myMethod;
{
obj = [BaseObj newObj]; //where newObj is a method like :[[[BaseObj alloc]init]autorelease];
}
-(void)anotherMehtod;
{
[obj someMeth]; //this sometimes gives me EXC_BAD_ACCESS
}
@end
So to solve this I put a retain. Now do I need to release this object manually if i retain it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您是对象的所有者 - 您有责任释放它。
如果您至少完成了以下其中一项操作,您就成为对象的所有者:
alloc
实例化对象retain
copy
了解更多详细信息阅读 对象所有权和处置
If you are the owner of an object - is your responsibility to release it.
You become owner of an object if you've done at least one of the following:
alloc
retain
copy
For more details read Object Ownership and Disposal
是的。规则是,如果您保留一个对象,您也有责任在 iOS 中释放它。
Yes. The rule is, if you retain an object, you are also responsible of releasing it in iOS.
与 Obj-C 中的所有其他静态方法一样,
[BaseObj newObj]
仅存在于该方法末尾的方法-(void)myMethod
中(大致)obj
从自动释放池获取-release
消息。如果您想保留此对象 - 使用
[[BaseObj newObj]retain]
或[BaseObj alloc] init]
并在-dealloc
中释放它或者当你必须这样做的时候。例如:
Like all other static methods in Obj-C
[BaseObj newObj]
only exists in your method-(void)myMethod
at the end of this method (roughly)obj
gets-release
message from autorelease pool.If you want to preserve this object - use
[[BaseObj newObj] retain]
or[BaseObj alloc] init]
and release it in-dealloc
or when you has to.For example: