• 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: Don Yacktman <email@hidden>
  • Date: Wed, 17 Jul 2002 11:39:32 -0600

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

Hi,

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

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

(While nil and NULL are both defined to be zero in the current implementation, "nil" is the proper return for an Objective-C non-object, not NULL. If the runtime ever changes something and nil is no longer zero, you'll be in a world of hurt if you've been using NULL. That's not likely to be happen, but I'd recommend using nil for objects and NULL for plain C pointers just the same.)

--
Later,

Don Yacktman
email@hidden
_______________________________________________
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: James DiPalma <email@hidden>
    • Re: Properly writing an Init method
      • From: Ali Ozer <email@hidden>
References: 
 >Properly writing an Init method (From: Pierre-Olivier Latour <email@hidden>)

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