According to naming conventions, you should call it +dataPoint
. The reason the analyzer is barfing at you is because (by convention), a class method that uses "new" is expected to return an object with a +1 retain count. You're returning an object with a +0 (autoreleased) retain count, and are therefore breaking convention.
As was alluded to in @Peter's answer, to create an empty object, it's usually done with [ClassName className]
methods. Ex: [NSArray array]
, [NSData data]
, [NSString string]
, etc. So the proper thing for you to do would be to rename it [DataPoint dataPoint]
and then implement it as return [[[self alloc] init] autorelease];
Using [self alloc]
is preferable, since it translates down to subclasses. IE, if you create a MutableDataPoint subclass, then (if you use [self alloc]
) doing [MutableDataPoint dataPoint]
returns a MutableDataPoint
, whereas if you leave it as [DataPoint alloc]
, it would come back as a DataPoint
object (because that's what you're allocating). (On a side note, self
refers to the object being operated on in the context of the method. For instance methods, it's the instance. For class methods, it's the Class
object itself)
Edit:
If you do want the class method to return an object with a +1 retain count, then (by convention), you'd just need to call it +new
(ie, DataPoint * p = [DataPoint new];
). However, use of the +new
method seems to have fallen into more-or-less disuse, with +alloc/-init...
or a class convenience method being the favorable alternatives.
Edit #2 (Re: Edit_003):
I personally prefer the former (+dataPoint
), but only because +new
isn't as common and I don't have to remember to -release
the object. Either one is perfectly acceptable and correct, though.
You are confusing the runtime and the compiler. The runtime has no problem coping with that. The issue is that dot notation (which is syntactic sugar) requires type information for the compiler to disambiguate between Objective-C objects and C structs.
If you don't use dot notation it works:
NSLog( @"%@ : %@\n", [exc name], [exc reason]) ;
The above will generate a warning if the type is not id since the compiler knows it does know the type and can't guarantee the dispatch will work, but it will compile and run.
Fundamentally the issue at hand is the compiler needs to know whether to generate a structure load, or an Objective C dispatch, in other words, with dot notation it needs to have enough information to determine the difference between an object and a scalar type.
Best Answer
Markus Müller's comment is correct, that is, your code is correct, except for the missing pool allocation and initialization.
However, if you create an autoreleased object in a routine/method, and you use that object in that routine/method, and you are done with it, before you exit that routine/method, then there is no reason to retain it and then release it. That object is guaranteed to be retained for the duration of the life of the routine/method, and, in this case, it will most likely be released by the [pool drain] method.
You are not going to have a situation where outerMost is released unexpectedly. The expected release is in [pool drain]. It doesn't matter how many other methods are called from within main(), as long as you are sicking to the retain/release guidelines, outerMost will not need to be retained.