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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/cocoadev%40mikeabdullah.net

This email sent to cocoa...@mikeabdullah.net

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to