• 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 on returning nil in init
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory management on returning nil in init


  • Subject: Re: Memory management on returning nil in init
  • From: Markus Hanauska <email@hidden>
  • Date: Mon, 21 Jun 2010 16:51:33 +0200

On Monday, 2010-06-21, at 16:43, Eiko Bleicher wrote:

> -(id) initWithBool:(BOOL)ok
> {
>   if (self = [super init])
>   {
>      if (!ok) {
>         return nil; // Point of interest
>      }
>   }
>   return self;
> }
>
> Does this code leak?

According to my understanding of Cocoa, it does leak. You should call [self release], as otherwise the just created object (the object has already been created by [CLASS alloc] when init is being called) is never released otherwise. Further no other object that [super init] might create is released. My inits usually look like this:

- (id)initWithBool:(BOOL)ok
{
  self = [super init];
  if (self) {
    if (!ok) {
      [self release];
      self = nil;
     }
  }
  return self;
}

--
Markus Hanauska




_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Memory management on returning nil in init
      • From: Eiko Bleicher <email@hidden>
References: 
 >Memory management on returning nil in init (From: Eiko Bleicher <email@hidden>)

  • Prev by Date: Re: Memory management on returning nil in init
  • Next by Date: Re: Memory management on returning nil in init
  • Previous by thread: Re: Memory management on returning nil in init
  • Next by thread: Re: Memory management on returning nil in init
  • Index(es):
    • Date
    • Thread