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 protected])

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 [email protected]

Reply via email to