What increases an object's retain count?

前端 未结 3 466
面向向阳花
面向向阳花 2020-12-03 08:27

Here is code I am referring to.

// Person.h

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

// Person.m

@implementa         


        
相关标签:
3条回答
  • 2020-12-03 08:40

    Retain counts are increased when you call alloc specifically, so you'll need to release that explicitly.

    factory methods usually give you an autoreleased object (such as [NSMutableArray array] -- you would have to specifically retain this to keep it around for any length of time.).

    As far as NSArray and NSMutableArray addObject:, someone else will have to comment. I believe that you treat a classes as black boxes in terms of how they handle their own memory management as a design pattern, so you would never explicitly release something that you have passed into NSArray. When it gets destroyed, its supposed to handle decrementing the retain count itself.

    You can also get a somewhat implicit retain if you declare your ivars as properties like @property (retain) suchAndSuchIvar, and use @synthesize in your implementation. Synthesize basically creates setters and getters for you, and if you call out (retain) specifically, the setter is going to retain the object passed in to it. Its not always immediately obvious, because the setters can be structured like this:

    Person fart = [[Person alloc] init];
    fart.firstName = @"Josh"; // this is actually a setter, not accessing the ivar
                              // equivalent to [fart setFirstName: @"Josh"], such that
                              // retainCount++
    

    Edit:

    And as far as the memory management, as soon as you add the object to the array, you're done with it... so:

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

    Josh

    0 讨论(0)
  • 2020-12-03 08:43

    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.

    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.

    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
    

    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];
    

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

    0 讨论(0)
  • 2020-12-03 08:43

    You should generally /not/ be worried about the retain count. That's internally implemented. You should only care about whether you want to "own" an object by retaining it. In the code above, the array should own the object, not you (outside of the loop you don't even have reference to it except through the array). Because you own [[Person alloc] init], you then have to release it.

    Thus

    Person *p = [[Person alloc] init];
    [array addObject:p];
    [p release];
    

    Also, the caller of "getPeople" should not own the array. This is the convention. You should autorelease it first.

    NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
    

    You'll want to read Apple's documentation on memory management: http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

    0 讨论(0)
提交回复
热议问题