I am trying to replace occurances of a string in a few different steps, I am using:
NSString *doc = [[NSString alloc] initWithData:htmlData encoding:NSASCIIStringEncoding];
doc = [doc stringByReplacingOccurrencesOfString:@"###DATA###" withString:cord];
doc = [doc stringByReplacingOccurrencesOfString:@"###NAME###" withString:ride.title];
doc = [doc stringByReplacingOccurrencesOfString:@"###DESC###" withString:ride.description];
[doc release];
I am first getting text from a file and wanting to replace a few string with me own. But, I am getting the following error when running this:
Program received signal: “EXC_BAD_ACCESS”.
[Switching to thread 13059]
Data Formatters temporarily unavailable, will re-try after a 'continue'. (Not safe to call dlopen at this time.)
I am not understanding why I am getting an EXC_BAD_ACCESS error. What am I trying to release that has already been released?!
When you assign to
docin the second line, you lose your reference to the string you originally created.In the last line, when you release the string, you’re not releasing the original. You’re releasing whatever it is now, which is the result of the line before that. So the original string leaks, you overrelease the replacement, and that overrelease causes a crash. Either autorelease your original string when you create it (and drop the final
releasecall) or use a different temporary for the modified strings you make in the middle.For example, change the code to:
And it won’t leak. If it still crashes after that, the crash will be from a different cause (such as you using the string later on without having held onto it somewhere.) GDB is your friend–check what it says in its backtrace.