iPhone-substringToIndex/substringFromIndex/substringWithRange内存泄漏 [英] iPhone - substringToIndex / substringFromIndex / substringWithRange memory leak
问题描述
仪器泄漏表明此代码泄漏:
Instruments leaks says that this code leaks:
NSString *name = [file substringToIndex:i];
Layer *actualLayer = nil;
for (Layer *lay in layers) {
if ([lay.layerName isEqual:name]) {
actualLayer = lay;
}
}
name是泄漏的对象.有一些奇怪的事情:它只会偶尔泄漏,而并非总是泄漏(在我的应用正常执行期间,该代码段执行了数百次,但泄漏仅3-4次).另一个奇怪的事情是,我认为名称对象是一个自动释放对象,并且从不明确保留或释放它,所以怎么可能泄漏了它?
name is the leaking object. There are some strange things: it only leaks sometimes, not always (this snippet of code is executed hundreds of time during a normal execution of my app, but it leaks just 3-4 times). The other strange thing is that i suppose the name object to be an autoreleasing object and it is never explicitaly retained or released, so how could it be that it's leaked?
看看堆栈,substringWithRange由substringToIndex调用,所以我认为问题出在substringToIndex方法中.
Taking a look at the stack, substringWithRange is called by substringToIndex, so the problem I think it's in the substringToIndex method.
推荐答案
泄漏偶尔会报告误报.您发布的代码看起来不错,因此请检查周围的代码是否可能引起问题.特别是,请使用Build&分析哪些可能会发现许多内存管理错误.如果您找不到问题,则有可能不存在该问题(在这种情况下,使用此特定工具).
Leaks occasionally reports false positives. The code you've posted looks fine, so check whether the surrounding code might be causing an issue. Particularly, use Build & Analyze which can pick up many memory management bugs. If you can't find the problem, there's a chance that it doesn't exist (in this particular case using this particular tool).
Bill Bumgarner describes another debugging tactic, using the heapshot instrument which can succeed where leaks fails.
这篇关于iPhone-substringToIndex/substringFromIndex/substringWithRange内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!