• 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: Martin Wierschin <email@hidden>
  • Date: Fri, 6 Feb 2009 17:22:43 -0800

On 2009.02.06, at 8:47 AM, Frédéric Testuz wrote:
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.

The only requirement the method has is to return an object that is valid for the caller. Because self is obviously valid, it's a perfectly legitimate return value. The fact that the caller releases that string after making the call is not any of the method's concern.


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

return [[self retain] autoreleased];

That would be nicer of it, but it's not gauranteed.

~Martin

_______________________________________________

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: Michael Ash <email@hidden>
References: 
 >Re: Simple memory problem (From: Steve Sisak <email@hidden>)
 >Re: Simple memory problem (From: Martin Wierschin <email@hidden>)
 >Re: Simple memory problem (From: Frédéric Testuz <email@hidden>)

  • Prev by Date: where's my exception?
  • Next by Date: Re: Stack Trace Using Google Toolbox for Mac
  • Previous by thread: Re: Simple memory problem
  • Next by thread: Re: Simple memory problem
  • Index(es):
    • Date
    • Thread