2010-11-13 73 views
0

我有這個代碼泄漏。性能工具泄漏告訴我,這是在這一行:泄漏從plist加載數據到CoreData

NSArray *fakeData = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FakeData" ofType:@"plist"]]; 

我找不出是怎麼回事。我加載的plist有3個NSDictionary元素,和截圖中的泄漏一樣。每個詞典有3個字符串。

整個代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
//Ładowanie danych 
if (![[FlickrFetcher sharedInstance] databaseExists]) { 
    NSArray *fakeData = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FakeData" ofType:@"plist"]]; 

    for (NSDictionary *element in fakeData) { 
     //Wypełnianie CoreData danymi 
     Photo *newPhoto = (Photo *)[NSEntityDescription insertNewObjectForEntityForName:@"Photo" 
                  inManagedObjectContext:[[FlickrFetcher sharedInstance] managedObjectContext]]; 
     NSLog(@"Creating Photo: %@", [element objectForKey:@"name"]); 
     [newPhoto setName:[element objectForKey:@"name"]]; 
     [newPhoto setImageURL:[element objectForKey:@"path"]]; 

     NSLog(@"Person is: %@", [element objectForKey:@"user"]);ŕŕŕ 
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", [element objectForKey:@"user"]]; 

     NSMutableArray *peopleArray = (NSMutableArray *)[[FlickrFetcher sharedInstance] fetchManagedObjectsForEntity:@"Person" 
                            withPredicate:predicate]; 
     NSEnumerator *enumerator = [peopleArray objectEnumerator]; 
     Person *person; 
     BOOL exists = FALSE; 

     while (person = [enumerator nextObject]) { 
      NSLog(@"Person is: %@", person.name); 
      if ([person.name isEqualToString:[element objectForKey:@"user"]]) { 
       exists = TRUE; 
       NSLog(@"-- Person exists: %@", person.name); 
       [newPhoto setOwner:person]; 
      } 
     } 

     if (!exists) { 
      Person *newPerson = (Person *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" 
                    inManagedObjectContext:[[FlickrFetcher sharedInstance] managedObjectContext]]; 
      [newPerson setName:[element objectForKey:@"user"]]; 
      NSLog(@"Person created: %@", newPerson.name); 
      [newPhoto setOwner:newPerson]; 
     } 

     NSError *error; 
     if (![[[FlickrFetcher sharedInstance] managedObjectContext] save:&error]) { 
      NSLog(@"Unresolved error %@ %@", error, [error userInfo]); 
      exit(-1); 
     } 

     [fakeData release];  
    } 

} 

//Person Navigation Controller 
personNavigationController = [[UINavigationController alloc] init]; 
PersonListViewController *personListViewController = [[PersonListViewController alloc] initWithStyle:UITableViewStylePlain]; 
personListViewController.title = @"Contacts"; 
[personNavigationController pushViewController:personListViewController animated:NO]; 
[personListViewController release]; 

//Recent Photo Navigation Controller 
recentPhotoNavigationController = [[UINavigationController alloc] init]; 
RecentPhotoViewController *recentPhotoViewController = [[RecentPhotoViewController alloc] init]; 
recentPhotoViewController.title = @"Recent"; 
UITabBarItem *item = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemRecents tag:1]; 
recentPhotoViewController.tabBarItem = item; 
[item release]; 
[recentPhotoNavigationController pushViewController:recentPhotoViewController animated:NO]; 
[recentPhotoViewController release]; 

//Tab Bar Controller 
tabBarController = [[UITabBarController alloc] init]; 
tabBarController.viewControllers = [NSArray arrayWithObjects: 
            personNavigationController, 
            recentPhotoNavigationController, 
            nil]; 


[window addSubview:tabBarController.view]; 
[window makeKeyAndVisible]; 

return YES; 

}

回答

0

它看起來像你的fakeData陣列被釋放的在for循環中,這似乎是有問題的幾個層次。您可能打算在循環退出時釋放它。從泄漏的角度來看,for循環可能永遠不會被輸入,在這種情況下,對象會被泄漏。