可变参数函数 - 调用 [obj release] 时的 EXC_BAD_ACCESS;
我有以下方法:
(void)makeString:(NSString *)str1,... {
va_list 字符串; NSString *innerText = [[NSString alloc] init]; NSString *tmpStr = [[NSString alloc] init]; 如果(str1){ va_start(字符串, str1); while (tmpStr = va_arg(字符串, id)) { innerText = [innerText stringByAppendingString:tmpStr]; } label.text = [str1 stringByAppendingString:innerText]; } [tmpStr 释放];
}
我最终会阅读《Objective C Memory Management》,我确信我会找到这个问题的答案 - 可能与指针和复制有关 - ,但是对于现在,任何人都可以解释为什么我添加 [innerText release];作为该函数的最后一行,我在运行时收到 EXC_BAD_ACCESS 错误?
I have the following method:
(void)makeString:(NSString *)str1,... {
va_list strings; NSString *innerText = [[NSString alloc] init]; NSString *tmpStr = [[NSString alloc] init]; if (str1) { va_start(strings, str1); while (tmpStr = va_arg(strings, id)) { innerText = [innerText stringByAppendingString:tmpStr]; } label.text = [str1 stringByAppendingString:innerText]; } [tmpStr release];
}
I will eventually get to Objective C Memory Management reading, where I'm sure I will find the answer to this - probably related to pointers and copying - , but for now, can anyone explain why if I add [innerText release]; as the last line of this function, i get an EXC_BAD_ACCESS error at runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您的代码是错误的。
据我所知,您只是连接字符串以将结果分配给
label.text
。我假设
label
是一个 ivar,因此label.text = ...
是合法的。然后应该执行以下操作:一些注意事项:
stringByAppendingString:
的结果,除非你之前已经保留过它。
[更新]
我更改了答案,因为它包含错误。
label.text = str1
当然应该保留str1
(如果它想保留的话)。特别是调用代码不应该不保留str1
,除非它想保留它自己。First, your code is erroneous.
As far as I can see you are only concatenating the strings to assign the result to
label.text
.I assume that
label
is an ivar, solabel.text = …
ist legal. Then the following should do it:Some notes:
stringByAppendingString:
unlessyou have retained it before.
[Update]
I changed the answer because it contained an error.
label.text = str1
should retainstr1
of course (if it wants to keep it). Especially the calling code should not retainstr1
unless it wants to keep it for itself.stringByAppendingString 返回一个自动释放的字符串,该字符串将替换您的原始分配。所以不需要你的释放。但是上面的两个分配会泄漏内存。
您可能也应该使用 [NSString initWithCString:va_arg(strings, id)] 来分配 tmpStr 。
stringByAppendingString returns an autoreleased string, which is replacing your original assignment. So your release is not needed. But you are leaking memory with the two allocs above.
You should probably use [NSString initWithCString:va_arg(strings, id)] to assign the tmpStr too.