views:

88

answers:

2

Can anyone tell me how I refer to these, is the first a system managed object and the second a user managed object, whats the terminology I should be using in objective-c / cocoa to refer to each of these?

01

+(Planet *) planet {
    gPlanetCount++;
    return [[[self alloc] init] autorelease];
}

int main(int argc, const char * argv[]) {
    Planet *outerMost;
    outerMost = [[Planet planet] retain]; // With
    ...
    ... some code
    ...
    [outerMost release]; 
    [pool drain];
    return 0;
}

// OR

int main(int argc, const char * argv[]) {
    Planet *outerMost;
    outerMost = [Planet planet]; // Without
    ...
    ... some code
    ...
    [pool drain];
    return 0;
}

02

+(Planet *) newPlanet {
    gPlanetCount++;
    return [[self alloc] init];
}

int main(int argc, const char * argv[]) {
    Planet *outerMost;

    outerMost = [Planet newPlanet];
    ...
    ... some code
    ...
    [outerMost release];
    [pool drain];
    return 0;
}

EDIT_001

So with the first example I would need to have something like this (text moved to 01 at the top)

EDIT_002

"Code cleaned up, revised final question below ..."

I am going to go with 01 (given that its the more usual way) can I ask again about the retain/release (i.e. if they are needed) this compiles and runs through the static analyser both with and without them?

gary

+3  A: 

In your first example, the result of +planet is autoreleased. Thus the caller must call -retain on the result if it wants to maintain a reference to the result. +planet is the more common pattern (although +[NSObject new] exists, it's much more common in Cocoa-land to use and alloc/init pair or a convenience constructor like your +planet (which returns an autoreleased instance according to the Cocoa memory management rules).

In both examples, the result of +planet/+newPlanet is an instance of the Planet class. There's no difference in terminology, but documentation of the (correct) first example might be explicit in stating the that the result is "autoreleased" even though the standard Cocoa memory management conventions would dictate that the result be autoreleased.

Barry Wark
As the second contains "new" I was assuming that calling outerMost = [Planet newPlanet]; followed at some point by [outerMost release]; would be correct as per the management rules?
fuzzygoat
Ooops. I didn't see the new in your second example. It *is* correct according to the management rules. I'll fix my answer.
Barry Wark
In Objective-C, you don't call methods, you send messages. The way this should be stated is, "Thus, the caller must send the result a -retain message".
NSResponder
@NSResponder You're correct. I find many Cocoa developers shorten "send the X message" to call X. Feel free to edit my post to make things more exact!
Barry Wark
+1  A: 

The actual object coming out of these methods is simply a Planet object instance, as Barry Wark says in his answer. However, the first method (+planet) would probably be referred to as a "convenience constructor".

EDIT_001

As I understand it, an autoreleased object will stay around for the duration of the method/function it was created in. You only need to retain the object if you want it to stay around longer than that.

Steve Harrison
An autoreleased object will stay around until the enclosing autorelease pool is drained/released. If you explicitly create one within a method, then this is the enclosing pool. Otherwise, the enclosing pool is either in a method lower in the call stack. Usually the enclosing pool will be drained at the end of the current NSRunLoop iteration.
Barry Wark
Ah ok I see, so as I am creating the object within the scope of main() I don't need to add the retain/release as it gets a +1 release count from the alloc (matched by the autorelease) when its created which in turn is disposed of correctly when main exits and drains the pool.
fuzzygoat
"An autoreleased object will stay around until the enclosing autorelease pool is drained/released."No.The autorelease pool is nothing but a delayed messaging mechanism. When you send -autorelease to an object, it will receive a -release message when the current pool is drained or released. That is ALL it means. The object in question may get destroyed before that, causing a memory error, or it may persist afterwards, if the -release message didn't take its retain count down to zero.
NSResponder