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

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

问题描述

这是我指的代码.

// 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,对吗?如果为2,我应该在将其添加到数组后将[remainCount]降为1,然后[p release]进入吗?

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的内存管理文档,但是我想我最不清楚的是,什么增加了对象的保留数量?不过,我认为我掌握了释放责任是谁的想法.根据苹果公司的说法,这是基本规则:

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的句子只担心您显式添加到该项目的保留计数"使它为我所用.在阅读了苹果公司的所有权政策后,实质上,创建新对象的对象/方法是负责释放其兴趣的对象/方法.这是正确的吗?

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

此外,正如我上面所说,您通常只在alloc或copy期间拥有对象,因此与另一端的一致,应返回autoreleased数组,以便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天全站免登陆