使用点语法设置保留属性时使用自动释放吗?
我在一些示例代码中看到使用了 autorelease
。我不熟悉需要这样做的情况。例如,如果我创建一个注释对象
头文件
@interface someViewController: UIViewController
{
Annotation *annotation;
}
@property (nonatomic, retain) Annotation *annotation;
@end
实现文件
@implementation someViewController
@synthesize annotation
@end
问题:如果我像这样在实现文件中初始化注释对象,这是正确的方法吗?
self.annotation = [[Annotation alloc] initWithCoordinate:location];
我需要为此设置自动释放吗?或者我可以按照正常方式进行操作并在 dealloc 方法中添加释放吗?
I see in some sample code that autorelease
is used. I am not familiar with the instances when this is required. For example, if I create an annotation object
Header file
@interface someViewController: UIViewController
{
Annotation *annotation;
}
@property (nonatomic, retain) Annotation *annotation;
@end
Implementation file
@implementation someViewController
@synthesize annotation
@end
Question: Is it the correct approach if I initialize my annotation object in the implementation file like this?
self.annotation = [[Annotation alloc] initWithCoordinate:location];
Do I need to set autorelease for this? Or can I just do it the normal way and add the release in the dealloc method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是正确的:
self.annotation = [[[Annotation alloc] initWithCooperative:location] autorelease];
因为注释属性被声明为保留属性,所以分配给它会增加其保留计数。
尽管如此,您还需要在
-dealloc
中释放 self.annotation。简而言之:
init 会将保留计数设置为 1;
分配给 self.annotation,将其设置为 2;
再次执行主循环时,autorelease会将其设置回1;
dealloc中的release会将retain count设置为0,从而该对象将被释放);
在我看来,考虑
autorelease
的最佳方式如下:autorelease
将为您的对象“安排”一个“自动”release
未来的某个(接近)点(通常是当控制流返回主循环时,但细节隐藏在苹果手中)。autorelease
通常与init
结合使用,特别是在以下情况下:当您
init
局部变量时,这样您就不需要不必在超出范围之前显式释放它(主循环将为您执行此操作);当您返回指向刚创建的对象的指针而不保留其所有权时(
create/make*
类型选择器的典型情况,接收者需要retain< /code> 它以获得所有权);
具有
保留
的属性,当您为它们分配一个它们应该唯一拥有的对象时;对于增加保留计数的数据结构(
NSMutableArray
、NSMutableDictionary
等):您通常应该autorelease
一个新的init
ed 对象,当您将其添加到此类数据结构时。除了情况 2 之外,很明显,使用
autorelease
是为了提高代码的可读性并减少出现错误的可能性(这意味着在所有其他情况下,您可以简单地在赋值后或作用域结束时显式释放您的对象)。
使用属性时,必须检查它们是否属于
retain
或assign
/copy
情况;在第一种情况下,将新初始化的对象分配给属性通常需要自动释放。无论如何,我建议至少浏览一下 iOS 内存管理。
this is correct:
self.annotation = [[[Annotation alloc] initWithCoordinate:location] autorelease];
because annotation property is declared as a retain property, so assigning to it will increment its retain count.
you will also need, all the same, to release self.annotation in
-dealloc
.in short:
init will set retain count to 1;
assigning to self.annotation, will set it to 2;
autorelease will set it back to 1 when the main loop is executed again;
release in dealloc will set the retain count to 0, so that the object will be deallocated);
the best way to think of
autorelease
is the following, in my opinion:autorelease
will "schedule" an "automatic"release
for your object at some (near) point in future (typically when the control flow goes back to the main loop, but details are hidden in the hands of Apple).autorelease
is mostly useful in conjunction withinit
, specifically in the following cases:when you
init
a local variable, so that you don't have torelease
it explicitly before it goes out of scope (the main loop will do that for you);when you return a pointer to an object you have just created without keeping ownership of it (typical case of the
create/make*
kind of selectors, the receiver is required toretain
it to get ownership);with properties that
retain
, when you assign to them an object that they should own uniquely;with data structures that increment the retain count (
NSMutableArray
,NSMutableDictionary
, etc): you should generallyautorelease
a newlyinit
ed object when you add it to such data structure.apart from case 2, it is evident that the use of
autorelease
is meant to improve readability of the code and reduce the potential for errors (meaning that in all of the other cases, you could simplyrelease
explicitly your object after the assignment or at the end of the scope).when using properties, you have always to check whether they are of the
retain
orassign
/copy
case; in the first case, assigning a newlyinit
ed object to a property generally requiresautorelease
.Anyway, I would suggest at least skimming one of the many tutorial on memory management for iOS.
自动释放是告诉对象在离开作用域之前释放自己。
有时,当你编码时,你会遇到类似这样的内容
//This is out of the range
当然
,释放对象并不意味着释放该对象。
有时,您保留该对象,以便仍然可以在其范围之外使用它。
从你的问题来看,如果你的对象位于你的头文件/接口中。
您应该在 dealloc 方法中释放它。 CMIIW。
Autorelease is telling the object to release itself before leaving the scope.
Sometimes when you code, you'll encounter something like this
//This is out of the scope
}
Of course, releasing an object doesn't mean deallocating the object.
Sometimes you retain the object so you can still use it outside of its scope.
Judging from your question, if your the object is located within your header file/interface.
You should release it in dealloc method. CMIIW.