一部の操作後に NSMutabledictionary を保持しても問題ないのに、作成時に保持するとリークが発生する理由を教えてください。
-(void) Func2
{
NSString *_errorDesc;
NSPropertyListFormat format;
NSString *_plistPath = [[NSBundle mainBundle] pathForResource:@"List" ofType:@"plist"];
NSData *_plistData = [[NSFileManager defaultManager] contentsAtPath:_plistPath];
NSDictionary *dataDict = (NSDictionary *) [NSPropertyListSerialization
propertyListFromData:_plistData
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&_errorDesc];
for (int i = 0; i<[dataDict count]; i++)
{
[_ups setObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:0],[NSNumber numberWithInt:0], nil]forKey:[NSString stringWithFormat:@"%d",i]];
}
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:_ups forKey:@"Ups"];
[defaults synchronize];
}
-(void) Func1
{
_ups = [NSMutableDictionary dictionary];
//_ups = [[NSMutableDictionary dictionary]retain]; = leak
//_ups = [[NSMutableDictionary alloc]init]; = leak
if(![[NSUserDefaults standardUserDefaults] objectForKey:@"Ups"])
{
[self Func2];
}else{
_ups = [[NSUserDefaults standardUserDefaults] objectForKey:@"Ups"];
}
[_ups retain]; // - ok
}
Instruments-> Leaks は、作成時に保持しようとしたときにのみリークが検出されたことを示していますが、辞書を埋めた後に保持すると問題ありません。
ありがとう。