Re: More memory allocation questions
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.