tags:

views:

42

answers:

2

Hi guys,

I am getting NSCFString for the code below:

NSString *urlString;

/*  leak showing below line */

urlString = [urlString stringByReplacingOccurrencesOfString:@"\n" withString:@""];

I am not getting how to solve this please can any one help me to solve this leak.

Thanks In advance.

A: 

Most likely you created your urlString using +alloc/initWith.... If that's the case then you own the urlString reference and are responsible for releasing it at some point. However, when you get to the line you pasted in the question, you're overwriting the urlString reference with a new string reference, since stringByReplacingOccurrencesOfString:withString: returns a new string object (ie, it doesn't modify the string in place).

My recommendation would be to use an NSMutableString instead, which does allow in-place string manipulation.

Dave DeLong
+1  A: 

You should do pointer allocation of string instead of static allocation. Change:

NSString urlString;

to:

NSString *urlString;

Also there seems to be some other code that initiates the urlString to some value to which you're doing the replace operation.

Eimantas
LOL I can't believe I didn't see that... whoops! :)
Dave DeLong
The compiler wouldn't let that compile anyway. It can't be the source of the leak (at least, not without screwing mightily with build settings).
bbum