我该如何解决这个内存泄漏问题?
问题描述:
这是我的代码片段:我该如何解决这个内存泄漏问题?
- (id) initWithFrame:(CGRect)frame andConfig:(PGParams*) params
{
for (int i=0; i<[conf.map count]; i++)
[conf.map replaceObjectAtIndex:i withObject:
[[NSString alloc] initWithFormat:@"%@&sito=%@",
[conf.map objectAtIndex:i], [params sito]]];
for (int i=0; i<[conf.orto count]; i++)
[conf.orto replaceObjectAtIndex:i withObject:
[[NSString alloc] initWithFormat:@"%@&sito=%@",
[conf.orto objectAtIndex:i], [params sito]]];
for (int i=0; i<[conf.mix count]; i++)
[conf.mix replaceObjectAtIndex:i withObject:
[[NSString alloc] initWithFormat:@"%@&sito=%@",
[conf.mix objectAtIndex:i], [params sito]]];
}
编译此代码与RUN_CLANG_STATIC_ANALYZER
选项(属性 - >编译选项 - >运行静态分析仪),它给我上[[NSString alloc] ...
泄漏。
RUN_CLANG_STATIC_ANALYZER
激活此设置将导致Xcode的运行符合条件的源文件锵静态分析工具。该工具目前支持C和Objective-C文件。 [RUN_CLANG_STATIC_ANALYZER]
我该如何解决呢?
由于事先
allberto
答
权。你正在分配一个你拥有的对象(因为你调用了+alloc
),但是你永远不会释放它。
您可以将[[NSString alloc] initWithFormat:...]
的所有实例替换为[NSString stringWithFormat:...]
以修复泄漏。
对!谢谢,现在已经修好了! – elp 2010-12-11 19:07:10