NSDictionary leak problem
I have a leak when my app is running on device. The leak is in the following code-snippet:
+ (NSMutableDictionary *)newDict:(int)index {
NSLog(@"%@: %s: %i", [self description],__FUNCTION__, index);
NSString *themePath;
NSDictionary *themesDict;
NSMutableArray *themesArray;
NSMutableDictionary *thisThemeDict;
if (index < 4) {
themePath = [[NSBundle mainBundle] pathForResource:PATH_THEMES_PLIST ofType:@"plist"];
themesDict = [NSDictionary dictionaryWithContentsOfFile:themePath];
themesArray = [[themesDict objectForKey:KEY_THEMES] mutableCopy];
thisThemeDict = [[themesArray objectAtIndex:index] mutableCopy];
} else {
themePath = [[NSBundle mainBundle] pathForResource:PATH_CHARTS_PLIST ofType:@"plist"];
themesDict = [NSDictionary dictionaryWithContentsOfFile:themePath];
themesArray = [[themesDict objectForKey:KEY_THEMES] mutableCopy];
thisThemeDict = [[themesArray objectAtIndex:0] mutableCopy];
}
themePath = nil;
themesDict = nil;
[themesArray release];
return thisThemeDict;
}
The leak-instrument highlighted the line:
themesDict = [NSDictionary dictionaryWithContentsOfFile:themePath];
The leaked object is a NSCFString, so I think the problem is with 'themePath'.
I tried several solutions for hours and hours... but without luck. Can anybody he开发者_如何学JAVAlp me out...
thanks xnz
You could use the llvm compiler (Build and analyze in xcode 3.1.2 and later, snow leopard) to locate the error specifically. If you are using Leopard (10.5) the leaks instrument reports non-existent memory leaks occasionally. I had the same issue with one of my projects. The new leaks instrument in Snow Leopard is like 100% better.
LLVM compiler
The code you've shown us is correct with regard to its management of retain counts. The one thing that could be getting leaked is thisThemeDict, which you return from this method with a +1 retain count. This means that wherever you call this method, you'll need to release the object it returns at some point. You should examine how you handle the object this method returns in every place that it is called in your codebase.
精彩评论