• 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
alloc, init and autorelease/release
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

alloc, init and autorelease/release


  • Subject: alloc, init and autorelease/release
  • From: Felix Schwarz <email@hidden>
  • Date: Sat, 22 Oct 2005 15:32:50 +0200

Hi everyone,

I just have a small question. Which of those implementations of init code are valid code?

Code sample 1:

    - (id) init
    {
        self = [super init];

        if (someErrorCondition)
        {
            [self autorelease];
            return (nil);
        }

        return (self);
    }


Code sample 2:

    - (id) init
    {
        if (someErrorCondition)
        {
            [self autorelease];
            return (nil);
        }
        else
        {
            // Calculation of someParameter goes here ..

            return ([self someOtherInit:someParameter]);
        }
    }

Or, in other words, is it ok to autorelease and or release an object right after "alloc"ation, before it was "init"ialized?

I.e. is it ok to do

    id someObj = [SomeObj alloc];
    [someObj release];

or

    id someObj = [SomeObj alloc];
    [someObj autorelease];

or does it *have to* be

    id someObj = [[SomeObj alloc] init];
    [someObj release];

to be error-free?

Best regards,
Felix
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: alloc, init and autorelease/release
      • From: j o a r <email@hidden>
  • Prev by Date: Re: How we can write console application using cocoa?
  • Next by Date: How to set width of "tab" (\t) in NSTextView?
  • Previous by thread: Re: How we can write console application using cocoa?
  • Next by thread: Re: alloc, init and autorelease/release
  • Index(es):
    • Date
    • Thread