Re: Beginner Question Re: Memory Management
Re: Beginner Question Re: Memory Management
- Subject: Re: Beginner Question Re: Memory Management
- From: WT <email@hidden>
- Date: Tue, 23 Jun 2009 07:24:58 +0200
On Jun 23, 2009, at 7:15 AM, Graham Cox wrote:
On 23/06/2009, at 2:31 PM, WT wrote:
[textFieldPreviousContent release];
textFieldPreviousContent = [textField.text retain];
This is not a safe pattern anyway (though it's not the same as the
discussion you raised). In isolation, suppose that the previous text
and the current text are the same object. If no-one else is
retaining it, then the first line will deallocate it, the second
will send a message to a now deallocated object, probably crashing.
You get away with it in this case because something else *is*
retaining the text, so the deallocation doesn't occur. But you
shouldn't be relying on this - you'll get fewer bugs if you treat
your memory management situations in isolation.
Excellent points.
so do something like this:
NSString* temp = [[textField.text] retain];
[previousText release];
previousText = temp;
Or
if (oldValue != newValue)
{
[oldValue release];
oldValue = [newValue retain];
}
Even better is to make the setting of the previous text a standalone
method, encapsulating this approach (or make it a retained property
and synthesise it, as you did). That way the ad-hoc memory
management done inline with other code can be removed, and isolated
into one, correct, method.
Yes, and that's why I'm using properties more and more often. I find
it distracting to have to stop my train of thought just to decide on
how I should manage memory at a particular section of code. I'd rather
make that decision only once, at the time I declare the property.
Wagner
_______________________________________________
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