• 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: Simple memory problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Simple memory problem


  • Subject: Re: Simple memory problem
  • From: Frédéric Testuz <email@hidden>
  • Date: Fri, 6 Feb 2009 17:47:10 +0100

Le 6 févr. 09 à 07:32, Martin Wierschin a écrit :

On Feb 5, 2009, at 4:42 PM, Steve Sisak wrote:

NSString * newString = [inputString stringByReplacingCharactersInRange:range withString:@""];
[inputString release]; // release old inputString
inputString = [newString retain]; // retain new inputString

This sequence is not safe. As an optimization there is nothing preventing "inputString" from returning self if "range" is of zero length. If that ever happened, you could end up deallocating the string before the following retain occurs. As has been suggested, using a single mutable string is probably the best way to go.

I don't think so. If I understand memory management correctly want you describe is a bug.


By the rule stringByReplacingCharactersInRange:withString: return an instance that the caller dont have ownership. And NSString know nothing about the ownership of self and the code outside of itself, he can't make this sort of optimization.

If there is an optimization in stringByReplacingCharactersInRange:withString: then return will have the form of :

return [[self retain] autoreleased];

With this sequence, the precedent code is safe.

Frédéric_______________________________________________

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: Simple memory problem
      • From: Martin Wierschin <email@hidden>
References: 
 >Re: Simple memory problem (From: Steve Sisak <email@hidden>)
 >Re: Simple memory problem (From: Martin Wierschin <email@hidden>)

  • Prev by Date: RE: NSTableView Popup Column issue
  • Next by Date: Re: Needed : set class for Cocoa
  • Previous by thread: Re: Simple memory problem
  • Next by thread: Re: Simple memory problem
  • Index(es):
    • Date
    • Thread