• 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: More memory allocation questions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: More memory allocation questions


  • Subject: Re: More memory allocation questions
  • From: Ondra Cada <email@hidden>
  • Date: Sun, 28 Jul 2002 23:36:48 +0200

On Sunday, July 28, 2002, at 11:22 , Ondra Cada wrote:

It seems that you should always use autorelease, unless you are 100% certain that nobody else is using a specified object, then release may be preferable because it will free up resources immediately.

Well... technically speaking with the only information you got in this list, yeah. Though, the "...keeps until end of this event" mantra is not quite right anyway; the right one says "keeps until the current pool is deallocated", and *that* might happen at the same time where you release.

Might need more light, after all:

The pattern

id o=[[Foo alloc] init];
[somebody something:o]; // code that never raises
[o release];

would be all right (nevertheless it very rarely occurs in properly written Cocoa code, see below why). If -something uses o without storing it, it's all right. If -something stores the o so that the value would survive your release, it should have retained it anyway -- so it's all right too.

The pattern would become wrong though as soon as -something can raise an exception (since then o would leak), and thus you should rarely -- if ever -- use it. The proper pattern for local variables is

id o=[[[Foo alloc] init] autorelease]; // of just o=[Foo foo], which is effectively the same
// don't care afterwards

which, as an added benefit, is more robust also since the creation and releasing is at the same place, making so much less probable you forget to release or release twice.
---
Ondra Hada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
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.
References: 
 >Re: More memory allocation questions (From: Ondra Cada <email@hidden>)

  • Prev by Date: Re: More memory allocation questions
  • Next by Date: Re: Serial number verification / obfuscation (was: Re: Hiding
  • Previous by thread: Re: More memory allocation questions
  • Next by thread: Re: More memory allocation questions
  • Index(es):
    • Date
    • Thread