views:

48

answers:

3

I am trying to understand what is happening in the getter below, this is what I understand so far:

(1) the getter returns a pointer to an NSString object. (2) the NSString object is retained, possibly because we have just given away a pointer to it. (3) autorelease? when does that activate, when the PlanetClass instance is deallocated (released)?

// CLASS
@interface PlanetClass : NSObject {
        NSString *planetName;
}
- (NSString *)planetName;
- (void)setPlanetName:(NSString *)value;
@end

// GETTER
- (NSString *)planetName{
        return[[planetName retain] autorelease];
}

EDIT: I think I am more confused regarding the reason for the actual retain and later release. my understanding was that the getter simply returned a pointer to either nil or an object that already exists (i.e. was set by the setter) I think I understand the retain as we are giving away a pointer and we need to track that, but what about the release, is that just a failsafe incase I later forget to release the NSString object?

The instance variable planetName is also release in my dealloc method (see below) autorelease seems to be doing the same, just later when the pool is drained?

- (void)dealloc {
        [planetName release];
        [super dealloc];
}

cheers -gary-

+1  A: 

When autorelease is sent to an object, it is added to the autorelease pool. When the pool is drained, it sends release to all the objects in the pool. So any object in the autorelease pool will be release when the pool is drained

ennuikiller
+4  A: 

It might be a good idea to let Objective-C handle this as a property, letting you clean up some of the implementation to keep the memory management, well, manageable:

@interface PlanetClass : NSObject {
    NSString* planetName;
}
@property(nonatomic, retain) NSString* planetName;
@end // PlanetClass

@implementation PlanetClass
@synthesize planetName
//... rest of PlanetClass here
@end // PlanetClass

There are plenty of docs available online for more details on Objective-C properties and @synthesize.

Memory Management Docs

I highly recommend this read from Apple on memory management to try and help understand what all the retain/release hubbub is about.

fbrereto
just spotter the bad wording and changed it to getter, pilot error sorry.
fuzzygoat
True, I am just trying to understand how things work, before skipping to a simpler method.
fuzzygoat
A: 

The return/autorelease in the getter method is not doing anything, you can just return planetName

kubi
That's not correct. retain followed by autorelease will ensure that the object in question won't get destroyed before the autorelease pool is drained.
NSResponder
True, but for the purposes of this thread, it's not going to make any difference. I assumed, based on the initial question, that the OP wasn't trying to do anything sneaky with the autorelease pool, they're just trying to get a property from an object.
kubi
It is very much relevant for the reasons that NSResponder stated.
bbum
How so? Under most circumstances how is the extra temporary retain count going to make any difference? I'm sure we can all come up with some specific instances where it will make a big difference, but those are hardly normal and are probably against all assumptions we make about the retain count of returned objects.
kubi