2010-12-11 58 views
0


這是我的代碼片段:我該如何解決這個內存泄漏問題?

- (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

回答

3

權。你正在分配一個你擁有的對象(因爲你調用了+alloc),但是你永遠不會釋放它。

您可以將[[NSString alloc] initWithFormat:...]的所有實例替換爲[NSString stringWithFormat:...]以修復泄漏。

+0

對!謝謝,現在已經修好了! – elp 2010-12-11 19:07:10