• 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: Properly writing an Init method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Properly writing an Init method


  • Subject: Re: Properly writing an Init method
  • From: Julian Barkway <email@hidden>
  • Date: Wed, 17 Jul 2002 20:24:35 +0200

On Wednesday, July 17, 2002, at 07:54 pm, Ali Ozer wrote:


- (id) init
{
if(self = [super init]) {
//initialize some stuff
//...

if(/* test something here*/)
return NULL; <- Memory leak?
}

return self;
}

If the initialization is not successful, I return NULL, however the memory
has already been allocated when I called [[MyClass alloc] init], so should I
add: [self dealloc] or [self release] before returning NULL in the init
method, or even [super dealloc]?

You want to do a [self release] before returning nil.

Actually, the init method which first fails should do the release; in this case, [super init] returned nil, so it should have released... You release only if you get success from the super, but then decide you can't init for some reason...


The above discussion is very interesting but there's also another point in the example code. I have often wondered about the merits of 'self = [super init]'. Surely this means that 'self' effectively becomes typed to the superclass rather than the class itself? My own feeling is that, other than in exceptional circumstances, assigning anything to 'self' is not a good idea. Anyone care to comment?
_______________________________________________
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.
  • Follow-Ups:
    • Re: Properly writing an Init method
      • From: Ondra Cada <email@hidden>
References: 
 >Re: Properly writing an Init method (From: Ali Ozer <email@hidden>)

  • Prev by Date: Re: [Seriously OT]Mac.com 60 days and counting
  • Next by Date: RE: [Seriously OT]Mac.com 60 days and counting
  • Previous by thread: Re: Properly writing an Init method
  • Next by thread: Re: Properly writing an Init method
  • Index(es):
    • Date
    • Thread