NSScanner 类别方法中的泄漏
我创建了一个 NSScanner 类别方法来显示仪器中的泄漏。
- (BOOL)scanBetweenPrefix:(NSString *)prefix
andSuffix:(NSString *)suffix
intoString:(NSString **)value
{
NSCharacterSet *charactersToBeSkipped = [self charactersToBeSkipped];
[self setCharactersToBeSkipped:nil];
BOOL result = NO;
// find the prefix; the scanString method below fails if you don't do this
if (![self scanUpToString:prefix intoString:nil])
{
MY_LOG(@"Prefix %@ is missing.", prefix);
return result;
}
//scan the prefix and discard
[self scanString:prefix intoString:nil];
// scan the important part and save it
if ([self scanUpToString:suffix intoString:value]) // this line leaks
{
result = YES;
}
[self setCharactersToBeSkipped:charactersToBeSkipped];
return result;
}
我认为这是我将值传递给方法或从方法传递值的方式,但我不确定。这是一个小泄漏(32 字节),但如果可以的话我想正确地做到这一点。提前致谢。
I created an NSScanner category method that shows a leak in instruments.
- (BOOL)scanBetweenPrefix:(NSString *)prefix
andSuffix:(NSString *)suffix
intoString:(NSString **)value
{
NSCharacterSet *charactersToBeSkipped = [self charactersToBeSkipped];
[self setCharactersToBeSkipped:nil];
BOOL result = NO;
// find the prefix; the scanString method below fails if you don't do this
if (![self scanUpToString:prefix intoString:nil])
{
MY_LOG(@"Prefix %@ is missing.", prefix);
return result;
}
//scan the prefix and discard
[self scanString:prefix intoString:nil];
// scan the important part and save it
if ([self scanUpToString:suffix intoString:value]) // this line leaks
{
result = YES;
}
[self setCharactersToBeSkipped:charactersToBeSkipped];
return result;
}
I figure it's the way I'm passing the value to/from the method, but I'm not sure. It's a small leak (32 bytes), but I'd like to do this right if I can. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了答案。我有一个模型类,它使用上面代码的结果,但忘记在 dealloc 方法中释放该属性。我应该用 Instruments 捕捉到它,但我不知道该去哪里寻找。我陷入了扩展详细信息中的调用堆栈中,其中只有我需要的部分信息。
对于像我这样的白痴,这就是我所做的:
您应该有一个 malloc 的释放版本和历史记录中的每个保留的释放版本。匹配你的保留和释放并寻找奇怪的东西。如有疑问,请查看页面底部并仔细检查您可能忘记在 dealloc 方法中释放的任何属性。
I found the answer. I had a model class that used the result of the code above, but forgot to release the property in the dealloc method. I should have caught it with Instruments, but I didn't know where to look. I got caught up in the call stack in the Extended Details, which had only part of the info I needed.
For morons like me, here's what I did:
You should have a release for the malloc and a release for every retain in the history. Match up your retains and releases and look for the oddball. When in doubt, look toward the bottom of the page and carefully review any properties you may have forgotten to release in your dealloc method(s).
好吧,我傻了。此方法不会泄漏。泄漏位于我的调用堆栈的更深处,但我不知道在哪里。抱歉过早发帖。如果我自己无法解决问题,当我弄清楚真正的泄漏在哪里时,我会跟进。
为了回答 Yuji 的评论,我的方法调用是:
是 &title 的问题吗?我根据现有的 NSScanner 方法和调用对其进行了模式化。
OK, I'm stupid. This method does not leak. The leak is farther down my call stack, but I can't figure out where. Sorry for the premature post. I'll follow up when I figure out where the real leak is, if I can't figure out the fix myself.
In answer to Yuji's comment, my method call is:
Is it the &title that's the problem? I patterned it after the existing NSScanner methods and calls.