iPhone SDK:替换字符串出现时出错

发布于 2024-10-02 14:03:49 字数 742 浏览 0 评论 0原文

我试图在几个不同的步骤中替换出现的字符串,我正在使用:

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];

我首先从文件中获取文本,并希望用我自己的字符串替换一些字符串。但是,运行此命令时出现以下错误:

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.)

我不明白为什么出现 EXC_BAD_ACCESS 错误。我想发布什么已经发布的内容?!

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?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏九 2024-10-09 14:03:49
NSString *doc = [[NSString alloc] initWithData:htmlData encoding:NSASCIIStringEncoding];
doc = [doc stringByReplacingOccurrencesOfString:@"###DATA###" withString:cord];

当您在第二行中分配给 doc 时,您将丢失对最初创建的字符串的引用。

[doc release];

在最后一行中,当您释放字符串时,您并没有释放原始字符串。你现在正在释放它,这是之前那行的结果。因此,原始字符串泄漏,您过度释放替换字符串,而过度释放会导致崩溃。要么在创建原始字符串时自动释放它(并删除最后的 release 调用),要么对中间修改的字符串使用不同的临时字符串。

比如把代码改成:

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];

就不会泄露了。如果此后它仍然崩溃,则崩溃将由不同的原因引起(例如您稍后使用该字符串而没有在某处保留它。)GDB 是您的朋友 - 检查它在回溯中所说的内容。

NSString *doc = [[NSString alloc] initWithData:htmlData encoding:NSASCIIStringEncoding];
doc = [doc stringByReplacingOccurrencesOfString:@"###DATA###" withString:cord];

When you assign to doc in the second line, you lose your reference to the string you originally created.

[doc release];

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 release call) or use a different temporary for the modified strings you make in the middle.

For example, change the 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文