Re: Beginner Question Re: Memory Management
Re: Beginner Question Re: Memory Management
- Subject: Re: Beginner Question Re: Memory Management
- From: Mike Abdullah <email@hidden>
- Date: Tue, 23 Jun 2009 10:42:34 +0100
Note also that you should really be using -copy for string properties.
It has the same effect as -retain, but also handles the possibility of
someone handing in an NSMutableString.
On 23 Jun 2009, at 06:15, 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.
so do something like this:
NSString* temp = [[textField.text] retain];
[previousText release];
previousText = temp;
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.
--Graham
_______________________________________________
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
_______________________________________________
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