Re: Memory Management Mismanaged
Re: Memory Management Mismanaged
- Subject: Re: Memory Management Mismanaged
- From: Danny Swarzman <email@hidden>
- Date: Thu, 8 May 2003 18:00:52 -0700
At 13:10 -0700 5/8/03, Clark S. Cox III wrote:
>
On Thursday, May 8, 2003, at 14:26 US/Eastern, Danny Swarzman wrote:
>
>
> At 3:00 -0700 5/8/03, Marcel Weiher wrote:
>
> [SNIP]
>
>>
>
>>> For example,
>
>>>
>
>>> + (MyFoo*) myFoo
>
>>> {
>
>>> MyFoo* aFoo = [[[myFoo alloc] init] autorelease];
>
>>> return aFoo;
>
>>> }
>
>>
>
>
>
> Is it '[MyFoo alloc]' ?
>
>
>
>
Better would be:
>
+ (MyFoo*) myFoo
>
{
>
MyFoo* aFoo = [[[self alloc] init] autorelease];
>
return aFoo;
>
}
>
>
>
>
>
>> Three minor nits:
>
>>
>
>> 1. Don't use a static return type, this will break for subclasses.
>
>> Use
>
>> id
>
>> 2. For the same reason, use 'self' to refer to the current class
>
>> 3. I don't think the temporary variable is buying you anything here,
>
>> so:
>
>>
>
>> +myFoo
>
>> {
>
>> return [[[self alloc] init] autorelease];
>
>> }
>
>>
>
>>
>
> Can you provide an example of how the first example breaks in a
>
> sub-class?
>
>
>
Example:
>
>
@interface MyFoo : NSObject {}
>
+(MyFoo*)myFoo;
>
@end
>
>
@interface MyBar : MyFoo {}
>
-(void)someMethodOnMyBar;
>
@end
>
>
>
....
>
[[MyBar myFoo] someMethodOnMyBar];
>
/*Compiler will complain, because return type of myFoo is (MyFoo*), and
>
MyFoo doesn't respond to -someMethodOnMyBar*/
>
>
--
Yeah. The compiler should complain. You wouldn't want to see this in code
that you take over from another programmer - unless you wanted a reason to
fire the person.
Use the compiler to help make the code more clear, not more obscure.
>
http://homepage.mac.com/clarkcox3/
>
email@hidden
>
Clark S. Cox, III
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.