• 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: Ondra Cada <email@hidden>
  • Date: Wed, 17 Jul 2002 22:35:00 +0200

On Wednesday, July 17, 2002, at 07:04 , Pierre-Olivier Latour wrote:

Say I have an init method like this one:

- (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]?

[self release] (or perhaps, to stay on the safe side, [self autorelease]) looks like the best bet, unless your -dealloc happens to be written so that it fails unless init was finished (which would be a bug anyway).

Besides, you should return nil, not NULL. For the compiler there's no difference, but for a human reader there is.
---
Ondra Cada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
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: 
 >Properly writing an Init method (From: Pierre-Olivier Latour <email@hidden>)

  • Prev by Date: Putting an Image Cell in NSTableView
  • Next by Date: Re: NSScreen visibleFrame
  • Previous by thread: Re: Properly writing an Init method
  • Next by thread: NSScreen visibleFrame
  • Index(es):
    • Date
    • Thread