nsstring - iPhone SDK: Errors when replacing string occurrences -
i trying replace occurances of string in few different steps, 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 first getting text file , wanting replace few string me own. but, getting following error when running this:
program received signal: “exc_bad_access”. [switching thread 13059] data formatters temporarily unavailable, re-try after 'continue'. (not safe call dlopen @ time.)
i not understanding why getting exc_bad_access error. trying release has been released?!
nsstring *doc = [[nsstring alloc] initwithdata:htmldata encoding:nsasciistringencoding]; doc = [doc stringbyreplacingoccurrencesofstring:@"###data###" withstring:cord];
when assign doc
in second line, lose reference string created.
[doc release];
in last line, when release string, you're not releasing original. you're releasing whatever now, result of line before that. original string leaks, overrelease replacement, , overrelease causes crash. either autorelease original string when create (and drop final release
call) or use different temporary modified strings make in middle.
for example, change code to:
nsstring *doc = [[[nsstring alloc] initwithdata:htmldata encoding:nsasciistringencoding] autorelease]; doc = [doc stringbyreplacingoccurrencesofstring:@"###data###" withstring:cord]; doc = [doc stringbyreplacingoccurrencesofstring:@"###name###" withstring:ride.title]; doc = [doc stringbyreplacingoccurrencesofstring:@"###desc###" withstring:ride.description];
and won't leak. if still crashes after that, crash different cause (such using string later on without having held onto somewhere.) gdb friend--check says in backtrace.
Comments
Post a Comment