Wie kann ich dieses Speicherleck beheben?
-
08-10-2019 - |
Frage
dies ist mein Snippet:
- (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]]];
}
Das Kompilieren dieses Codes mit RUN_CLANG_STATIC_ANALYZER
Option ( Property-> Build-Optionen-> Run Static Analyzer ), es zeigt mir ein Leck auf [[NSString alloc] ...
.
RUN_CLANG_STATIC_ANALYZER
diese Einstellung aktivieren wird Xcode verursacht den Clang statische Analyse-Tool auf der Qualifikation Quelldateien auszuführen. Dieses Tool unterstützt derzeit C und Objective-C-Dateien. [RUN_CLANG_STATIC_ANALYZER]
Wie kann ich es beheben?
Vielen Dank im Voraus,
allberto
Lösung
Right. You're allocating an object that you own (because you invoked +alloc
), but then you're never releasing it.
You can replace all instances of [[NSString alloc] initWithFormat:...]
with [NSString stringWithFormat:...]
to fix the leak.