• 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: Proper retain/release etiquette
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Proper retain/release etiquette


  • Subject: Re: Proper retain/release etiquette
  • From: Gregory Weston <email@hidden>
  • Date: Tue, 23 Jul 2002 06:59:30 -0700 (PDT)

Roarke Lynch <email@hidden> wrote

>On Monday, July 22, 2002, at 09:05 PM, Chris Anderson wrote:
>
>> 2. The right place to release contained objects is in the dealloc
>> method.
>> They should be released and then a [super dealloc] should be done.
>
> Yes, unless you are changing the 'nature' of a contained object. i.e
>
> - (void)setVal:(id)newVal {
> [val autorelease];
> val = [newVal copy];
> }

And be sure you're aware of the different behavior of [newVal copy] and [newVal retain]. Either may be desirable depending on your goals.

>> 4. It's OK to release objects that were allocated by another thread
>> (assuming that the other thread properly retained what it still needs.)
>
> I find it generally safer not to, but if you do. Call autorelase
>not release. This delays the release call until the end of the run loop,
>allowing the object to still be used in other areas of the app.

Which shouldn't be an issue if those "other areas" have properly retained the object.


>> 5. Similarly, auto-release pools in multiple threads won't auto-collide
>> even if they contain some of the same objects.
>
> I believe that there should generally only be one autorelease pool,
>special cases excluded.

Multiple threads would be one of those "special cases." They _must_ have their own autorelease pool. Similarly, you may choose to introduce a new one in certain looping situations.


>> 9. One should not attempt to re-use objects by re-initing them.
>
> no.

Your response is ambiguous, but the OP is correct.

>> 10. It is bad practice to do: id bar = [Foo alloc]; [bar init]; because
>> init might hand back a different object than the original alloc did.
>
> This is programmatically similar to nesting the two i.e id bar =
>[[Foo alloc] init]. So i think it makes no difference.

Actually, it's _not_ programmatically similar, for exactly the reason the OP mentioned. In "id bar = [[Foo alloc] init];" bar is assigned the result of the init method. In "id bar = [Foo alloc]; [bar init];" bar is assigned the result of an alloc method. The result of the init method - which may not be the receiver - is thrown away.

G
_______________________________________________
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: Proper retain/release etiquette
      • From: Daryn <email@hidden>
    • Re: Proper retain/release etiquette
      • From: Philip George <email@hidden>
  • Prev by Date: Re: NSString stringWithContentsOfURL
  • Next by Date: Re: [Q] How to frame NSTableView and NSOutlineView?
  • Previous by thread: Re: Proper retain/release etiquette
  • Next by thread: Re: Proper retain/release etiquette
  • Index(es):
    • Date
    • Thread