• 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: Steve Sisak <email@hidden>
  • Date: Fri, 6 Feb 2009 09:18:25 -0500

At 1:32 PM +0000 2/6/09, harry greenmonster wrote:
But why can the compiler not replace the data at the memory location pointed to by the old 'inputString' without making a second pointer (with the same name). This would then pose no problems in the scenarios you put forward.?

If you use an Objective-C 2.0 property (rather than a variable) and specifying retain semantics, you'll get that behavior -- with a bit of overhead.


At 12:35 PM +0000 2/6/09, Jeremy Pereira wrote:
On 6 Feb 2009, at 06:32, Martin Wierschin wrote:
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.

So retain newString before releasing inputString - or explicitly test for pointer equality and only do the release and retain if they are not equal.

Forgetting releasing before retain was a bug -- as a rule you always want to retain before release.


This is especially important with operations on arrays and dictionaries as elements could be shared between the source and result (testing for equality won't help you here).

So there's a complete copy:

while([inputString isMatchedByRegex:regexString])
{
	NSAutoreleasePool* pool = [NSAutoreleasePool new];

	range = [inputString rangeOfRegex:regexString];

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

	[inputString release]; // release old inputString

	inputString = newString;

	[pool drain];
}


(the error was probably instructive in this conversation, however)

_______________________________________________

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: harry greenmonster <email@hidden>
References: 
 >Re: Simple memory problem (From: Scott Ribe <email@hidden>)
 >Re: Simple memory problem (From: harry greenmonster <email@hidden>)
 >Re: Simple memory problem (From: Bryan Henry <email@hidden>)
 >Re: Simple memory problem (From: harry greenmonster <email@hidden>)

  • Prev by Date: Re: Simple memory problem
  • Next by Date: Re: Simple memory problem
  • Previous by thread: Re: Simple memory problem
  • Next by thread: Re: Simple memory problem
  • Index(es):
    • Date
    • Thread