• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Memory Management Mismanaged
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory Management Mismanaged


  • Subject: Re: Memory Management Mismanaged
  • From: Marcel Weiher <email@hidden>
  • Date: Thu, 8 May 2003 21:52:50 +0200

+ (MyFoo*) myFoo
{
MyFoo* aFoo = [[[myFoo alloc] init] autorelease];
return aFoo;
}


Is it '[MyFoo alloc]' ?

Probably, but you'd have to ask the author of those lines of code, not me...

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?

The first example will return an instance of the superclass, not of the subclass.

MyFoo* newFoo = [[MyFoo myFoo] retain];

Possibly easier:

[self setFoo:[MyFoo myFoo]];


Wouldn't this snippet be likely to appear outside of the definition of
MyFoo?

Yes.

So who is 'self' here?

Some object that wishes to hold onto an instance of MyFoo. If you need to retain, it is almost invariably because you need to set an instance variable of an object.

Marcel


--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
_______________________________________________
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.

References: 
 >Re: Memory Management Mismanaged (From: Danny Swarzman <email@hidden>)

  • Prev by Date: adding a login item programmatically
  • Next by Date: Strange error
  • Previous by thread: Re: Memory Management Mismanaged
  • Next by thread: Re: Memory Management Mismanaged
  • Index(es):
    • Date
    • Thread