什么增加了对象的保留计数? [英] What increases an object's retain count?

查看:27
本文介绍了什么增加了对象的保留计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我指的代码.

// Person.h

@interface Person : NSObject {
    NSString *firstName;
    NSString *lastName;
}
@end

// Person.m

@implementation Person
- (id)init {
    if (![super init]) return nil;
    firstName = @"John";
    lastName = @"Doe";
}
@end

// MyClass.m

@implementation MyClass
    .....
- (NSArray *)getPeople {
    NSMutableArray *array = [[NSMutableArray alloc] init];

    int i;
    for (i = 0; i < 10; i++) {
        Person *p = [[Person alloc] init];
        [array addObject:p];
    }

    return array;
}
    .....
@end

现在,我知道此示例代码中没有进行内存管理.需要什么?

Now, I know there is no memory-management going on in this sample code. What would be required?

在 getPeople 循环中,我分配了一个 Person (retainCount 1),然后将其添加到数组中.保留计数现在是 2,对吗?如果它是两个,我应该在将它添加到数组后 [p release]'ing,将 retainCount 恢复为 1 吗?

In the getPeople loop, I am alloc'ing a Person (retainCount 1), then adding it to array. The retain count is now 2, right? If it is two, should I be [p release]'ing after adding it to the array, bringing the retainCount back down to 1?

我是否正确地认为释放方法返回的数组是调用者的责任?(这也将释放 Person 及其实例变量的内存,假设它们的计数为 1).

Am I right in that it is the caller's responsibility to release the array returned by the method? (Which would also free the memory of the Person's, and their instance variables, assuming their counts are at 1).

我已经阅读了 Apple 的内存管理文档,但我想我最不清楚的是,是什么增加了对象保留计数?不过,我想我明白应该由谁负责释放的想法.根据 Apple 的说法,这是基本规则:

I have read Apple's memory management document, but I guess what I am most unclear about, is what increases an objects retain count? I think I grasp the idea of who's responsibility it is to release, though. This is the fundamental rule, according to Apple:

如果您使用名称以alloc"或new"开头或包含copy"(例如,alloc、newObject 或 mutableCopy)的方法创建对象,或者如果您向它发送一个保留消息.您有责任使用 release 或 autorelease 放弃您拥有的对象的所有权.其他任何时候你收到一个对象,你不能释放它.

You take ownership of an object if you create it using a method whose name begins with "alloc" or "new" or contains "copy" (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it.

bobDevil 的一句话只担心你明确添加到项目中的保留计数"让我点击了它.在阅读 Apple 的所有权政策后,本质上,创建新对象的对象/方法是负责释放/它的/兴趣的对象/方法.这是正确的吗?

bobDevil's sentence "only worry about the retain counts you add to the item explicitly" made it click for me. After reading the Ownership policy at Apple, essentially, the object/method that created the new object, is the one responsible for releasing /it's/ interest in it. Is this correct?

现在,假设我有一个方法,它接收一个对象,并将其分配给一个实例变量.我需要正确保留接收到的对象,因为我仍然对它感兴趣?

Now, let's say I a method, that receives an object, and assigns it to a instance variable. I need to retain the received object correct, as I still have an interest in it?

如果有任何不正确的地方,请告诉我.

If any of this is incorrect, let me know.

推荐答案

将其添加到数组后,保留计数为 2,这是正确的.但是,您应该只担心显式添加到项目中的保留计数.

You are correct that the retain count is 2 after adding it to an array. However, you should only worry about the retain counts you add to the item explicitly.

保留一个对象是一个合同,上面写着我和你没完,别走."一个基本的经验法则(有例外,但它们通常被记录在案)是当您分配一个对象或创建一个副本时,您拥有该对象.这意味着您将获得保留计数为 1(未自动释放)的对象.在这两种情况下,您应该在完成后释放它.此外,如果您明确保留了一个对象,则必须释放它.

Retaining an object is a contract that says "I'm not done with you, don't go away." A basic rule of thumb (there are exceptions, but they are usually documented) is that you own the object when you alloc an object, or create a copy. This means you're given the object with a retain count of 1(not autoreleased). In those two cases, you should release it when you are done. Additionally, if you ever explicitly retain an object, you must release it.

因此,具体到您的示例,当您创建 Person 时,您有一个保留计数.你将它添加到一个数组中(它可以做任何事情,你不在乎)然后你完成了 Person,所以你释放它:

So, to be specific to your example, when you create the Person, you have one retain count on it. You add it to an array (which does whatever with it, you don't care) and then you're done with the Person, so you release it:

Person *p = [[Person alloc] init]; //retain 1, for you
[array addObject:p]; //array deals with p however it wants
[p release]; //you're done, so release it

另外,正如我上面所说的,您通常只在分配或复制期间拥有对象,因此为了与事物的另一侧保持一致,您应该返回自动释放的数组,以便 getPeople 方法的调用者不拥有它.

Also, as I said above, you only own the object during alloc or copy generally, so to be consistent with that on the other side of things, you should return the array autoreleased, so that the caller of the getPeople method does not own it.

return [array autorelease];

正确,如果你创建它,你必须释放它.如果您(通过保留)对其投资兴趣,您必须释放它.

Correct, if you create it, you must release it. If you invest interest in it (through retain) you must release it.

这篇关于什么增加了对象的保留计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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