使用点语法设置retain属性时,使用autorelease? [英] Use autorelease when setting a retain property using dot syntax?

查看:142
本文介绍了使用点语法设置retain属性时,使用autorelease?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些示例代码中看到使用 autorelease 。我不熟悉的实例,当这是必需的。例如,如果我创建一个注释对象



头文件

  @interface someViewController:UIViewController 
{
注释*注释;
}

@property(nonatomic,retain)注释*注释;

@end

实施文件

  @implementation someViewController 
@synthesize注释
@end

$问题:如果我在这样的实现文件中初始化我的注释对象,它是正确的方法吗?

  self.annotation = [[注释alloc] initWithCoordinate:location]; 

我需要为此设置autorelease吗?

解决方案

这是正确的:



self.annotation = [[[Annotation alloc] initWithCoordinate:location] autorelease];



因为注释属性声明为retain属性,所以分配给它将增加其保留计数。



您还需要在 -dealloc 中发布self.annotation。



简短:


  1. init会将保留计数设置为1;


  2. autorelease将其设置为自动注释当再次执行主循环时,为1;


  3. release在dealloc中会将retain计数设置为0,以便对象被释放);


想想 autorelease 的最佳方法是我的意见: autorelease 会在将来的某个(近)点为您的对象计划一个自动发布 (通常当控制流回到主循环,但细节隐藏在苹果手中)。



autorelease 主要用于 init ,特别是在以下情况下:


    <当你 init 一个局部变量时,你不需要 release 它会超出范围(主循环会为你做);


  1. 当你返回一个指针指向你刚刚创建的对象,它(典型情况是 create / make * 种选择器),接收者需要保留 );当您向他们分配他们应该拥有唯一的对象时,保留


  2. < ;


  3. 使用增加保留计数的数据结构( NSMutableArray NSMutableDictionary ,etc):你应该通常 autorelease 一个新的 init


除了第2种情况,很明显使用 autorelease 是为了提高代码的可读性并减少错误的可能性(意思是在所有其他情况下,你可以简单地 release 你的对象在赋值后或在作用域的结尾)。



当使用属性时,你总是要检查它们是否是 retain assign / copy 在第一种情况下,将一个新的 init ed对象赋值给属性通常需要 autorelease



反正,我建议您至少略过 iOS的内存管理


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?

解决方案

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:

  1. init will set retain count to 1;

  2. assigning to self.annotation, will set it to 2;

  3. autorelease will set it back to 1 when the main loop is executed again;

  4. 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 with init, specifically in the following cases:

  1. when you init a local variable, so that you don't have to release it explicitly before it goes out of scope (the main loop will do that for you);

  2. 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 to retain it to get ownership);

  3. with properties that retain, when you assign to them an object that they should own uniquely;

  4. with data structures that increment the retain count (NSMutableArray, NSMutableDictionary, etc): you should generally autorelease a newly inited 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 simply release 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 or assign/copy case; in the first case, assigning a newly inited object to a property generally requires autorelease.

Anyway, I would suggest at least skimming one of the many tutorial on memory management for iOS.

这篇关于使用点语法设置retain属性时,使用autorelease?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆