使用点语法设置保留属性时使用自动释放吗?

发布于 2024-11-08 12:29:43 字数 549 浏览 0 评论 0原文

我在一些示例代码中看到使用了 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 技术交流群。

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

发布评论

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

评论(2

痴意少年 2024-11-15 12:29:43

这是正确的:

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

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

尽管如此,您还需要在 -dealloc 中释放 self.annotation。

简而言之:

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

  2. 分配给 self.annotation,将其设置为 2;

  3. 再次执行主循环时,autorelease会将其设置回1;

  4. dealloc中的release会将retain count设置为0,从而该对象将被释放);

在我看来,考虑 autorelease 的最佳方式如下: autorelease 将为您的对象“安排”一个“自动”release未来的某个(接近)点(通常是当控制流返回主循环时,但细节隐藏在苹果手中)。

autorelease 通常与 init 结合使用,特别是在以下情况下:

  1. 当您 init 局部变量时,这样您就不需要不必在超出范围之前显式释放它(主循环将为您执行此操作);

  2. 当您返回指向刚创建的对象的指针而不保留其所有权时(create/make* 类型选择器的典型情况,接收者需要retain< /code> 它以获得所有权);

  3. 具有保留的属性,当您为它们分配一个它们应该唯一拥有的对象时;

  4. 对于增加保留计数的数据结构(NSMutableArrayNSMutableDictionary等):您通常应该autorelease一个新的inited 对象,当您将其添加到此类数据结构时。

除了情况 2 之外,很明显,使用 autorelease 是为了提高代码的可读性并减少出现错误的可能性(这意味着在所有其他情况下,您可以简单地 在赋值后或作用域结束时显式释放您的对象)。

使用属性时,必须检查它们是否属于 retainassign/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:

  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.

苏别ゝ 2024-11-15 12:29:43

自动释放是告诉对象在离开作用域之前释放自己。

有时,当你编码时,你会遇到类似这样的内容

- (void)doSomething
{
    if(true)
    {
        NSString *foo = [[NSString alloc] initWithString:@"foo"];

        //Some execution here

        [foo release];
    }
}

- (void)doSomething
{
    if(true)
    {
        //By doing this is telling to to release foo object before getting out of the scope
        //which is similar with above practice
        NSString *foo = [[[NSString alloc] initWithString:@"foo"] autorelease];

        //Or you can do it this way
        NSString *foo = [[NSString alloc] initWithString:@"foo"];
        [foo autorelease];

        //Some execution carry on, it'll release foo before entering next scope

    }

//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

- (void)doSomething
{
    if(true)
    {
        NSString *foo = [[NSString alloc] initWithString:@"foo"];

        //Some execution here

        [foo release];
    }
}

- (void)doSomething
{
    if(true)
    {
        //By doing this is telling to to release foo object before getting out of the scope
        //which is similar with above practice
        NSString *foo = [[[NSString alloc] initWithString:@"foo"] autorelease];

        //Or you can do it this way
        NSString *foo = [[NSString alloc] initWithString:@"foo"];
        [foo autorelease];

        //Some execution carry on, it'll release foo before entering next scope

    }

//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.

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