C 方法中的 Objective-C 内存泄漏
大家好 每当我触发此方法时,我都会在泄漏工具上遇到内存泄漏。你觉得这有什么问题吗?
static NSString* readStringOrDie(InputData *input, size_t length, NSStringEncoding encoding)
{
NSString *str = [[NSString alloc] initWithBytes: readOrDie(input,length)
length: length
encoding: encoding]; // HERE THE LEAK COMES !!
if (!str)
[NSException raise: BERParserException format: @"Unparseable string"];
return [str autorelease];
}
Hi all
I get memory leaks on the leaks instrument whenever I trigger this method. Do you think anything is wrong with this??
static NSString* readStringOrDie(InputData *input, size_t length, NSStringEncoding encoding)
{
NSString *str = [[NSString alloc] initWithBytes: readOrDie(input,length)
length: length
encoding: encoding]; // HERE THE LEAK COMES !!
if (!str)
[NSException raise: BERParserException format: @"Unparseable string"];
return [str autorelease];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那个代码没问题。最有可能的是,接收到 str 的 actor 会保留它,但随后无法释放它。 Instruments 知道泄漏的内存分配在哪里,但不知道应该在哪里释放,因此它可以提供一些稍微无用的线索。
That code is fine. Most likely the actor that receives str retains it and subsequently fails to release it. Instruments knows where memory that has leaked was allocated but doesn't know where it should have been released, so it can provide some slightly unhelpful clues.
因为使用 NSException 来处理可恢复的错误。
Because of using NSException for recoverable error.