• 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: Simple instance [[alloc] init] question.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Simple instance [[alloc] init] question.


  • Subject: Re: Simple instance [[alloc] init] question.
  • From: Dave Geering <email@hidden>
  • Date: Tue, 31 Aug 2010 14:36:29 +1000

>>> // 1)
>>> self.serialIDs = [[IRMSerialDetailsDO alloc] init];
>>
>> The alloc method allocates an instance with a retain count of 1, and
>> assigning it to the serialIDs property bumps it up to 2. In your
>> dealloc method, you will [hopefully] send it a release message which
>> puts it back at 1, but this means the object still survives (i.e. it
>> is not deallocated).
>>
>>> // 2)
>>> IRMSerialDetailsDO *mySerialIDDO = [[IRMSerialDetailsDO alloc] init];
>>> self.serialIDDO = mySerialIDDO;
>>> [mySerialIDDO release];
>>
>> Here, the alloc creates the instance with a retain count 1, assigning
>> it to the property bumps it up to 2, and the release right afterwards
>> puts it back down to 1. In your dealloc method, you will sent it
>> another release message which puts it at 0, and therefore the instance
>> is deallocated.
>>
>
> This is at best misleading, and illustrates why people are typically discouraged from reformulating the memory management rules.
>

I apologise. I was going to explain each one in terms of ownership,
but I couldn't figure out a way to explain how you own something twice
without talking about reference counts. I should probably refrain from
replying to the list any time in the morning.
_______________________________________________

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: Simple instance [[alloc] init] question.
      • From: Kyle Sluder <email@hidden>
References: 
 >Simple instance [[alloc] init] question. (From: "Frederick C. Lee" <email@hidden>)
 >Re: Simple instance [[alloc] init] question. (From: Dave Geering <email@hidden>)
 >Re: Simple instance [[alloc] init] question. (From: mmalc Crawford <email@hidden>)

  • Prev by Date: Re: Simple instance [[alloc] init] question.
  • Next by Date: Re: Style question
  • Previous by thread: Re: Simple instance [[alloc] init] question.
  • Next by thread: Re: Simple instance [[alloc] init] question.
  • Index(es):
    • Date
    • Thread