Re: Should I retain a variable returned from this accessor?
Re: Should I retain a variable returned from this accessor?
- Subject: Re: Should I retain a variable returned from this accessor?
- From: Negm-Awad Amin <email@hidden>
- Date: Tue, 12 Aug 2008 14:45:03 +0200
Am Di,12.08.2008 um 14:37 schrieb Sean DeNigris:
Hi, how do I handle memory management for todoUid below? Do I
have to
retain or autorelease it?
[...snip...]
// Get uid to return
NSString* todoUid = [newTodo uid];
[...snip...]
return todoUid;
}
you could do:
NSString* todoUid = [[[newTodo uid] retain] autorelease];
YES! Many, many of memory managemt problems are solved with this
technique. BTW: You can do the same inside a getter to get rid of
thread problems.
Is the performance hit of keeping objects around until the next pool
drain (instead of releasing them when you're done with them)
insignificant?
In most cases: Yes. You can recognize the other cases after checking
it. (Insert quotation of Donald Knuth here).
More: As I wrote, releasing the allocated object let this object
disappear. It does not free any object that had been allocated in the
autorelease pool, when the released object was constructed. The
isolated -release is a king of prying.
So the solution is not to send some release-messages to anything
aorund, trying to hit one or another object, but using a custom ARP.
In other words, should it be a practice to always autorelease with
alloc and init (except for loops as previously mentioned)?
Amin Negm-Awad
email@hidden
_______________________________________________
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