Re: [newbie] NSString code so it won't leak
Re: [newbie] NSString code so it won't leak
- Subject: Re: [newbie] NSString code so it won't leak
- From: jean Bousquet <email@hidden>
- Date: Wed, 11 Sep 2002 15:58:55 +0200
le 10/09/02 17:12, email@hidden `
email@hidden a icrit :
>
In languages I'm used to you could write code like...
>
>
myString = "SOME BIG LONG ... STRING"
>
string1 = left(myString,40)
>
myString = mid(myString,41)
>
>
Thereby isolating the first 40 characters to string1
>
and the balance going back into myString
>
>
In Cocoa I was using...
>
NSString *string1,*myString
>
>
string1 = [myString stringToIndex:41];
>
myString = [myString stringFromIndex:41];
>
>
This works but leaks.
>
So what's better?
>
All I've come up with is
>
string1 = [myString stringToIndex:41];
>
myString2 = [myString stringFromIndex:41];
>
[myString release];
>
myString = [myString2 stringFromString:myString2];
>
// or? myString = [myString2 copy];
>
[myString2 release];
>
>
Would simply making myString as a NSMutableString make
>
the first code section not leak?
>
>
Dean Davis
IMHO myString is a pointer. With the instruction : myString = [myString
stringFromIndex:41] you redefine it but the previous one is lost (should
leak).
You could try that way :
NSString *string1;
NSMutableString *myString;
string1 = [myString stringToIndex:41];
[myString setString:[myString stringFromIndex:41]];
Jean Bousquet
COGILOG
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.