• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Beginner Question Re: Memory Management
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Beginner Question Re: Memory Management


  • Subject: Re: Beginner Question Re: Memory Management
  • From: Graham Cox <email@hidden>
  • Date: Tue, 23 Jun 2009 15:15:07 +1000


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


  • Follow-Ups:
    • Re: Beginner Question Re: Memory Management
      • From: Mike Abdullah <email@hidden>
    • Re: Beginner Question Re: Memory Management
      • From: WT <email@hidden>
References: 
 >Beginner Question Re: Memory Management (From: Daniel Torrey <email@hidden>)
 >Re: Beginner Question Re: Memory Management (From: Graham Cox <email@hidden>)
 >Re: Beginner Question Re: Memory Management (From: WT <email@hidden>)

  • Prev by Date: Re: Beginner Question Re: Memory Management
  • Next by Date: Re: Beginner Question Re: Memory Management
  • Previous by thread: Re: Beginner Question Re: Memory Management
  • Next by thread: Re: Beginner Question Re: Memory Management
  • Index(es):
    • Date
    • Thread