【问题标题】:Leak with Mutable Array even when released即使在释放时也使用可变数组泄漏
【发布时间】:2011-05-15 14:49:58
【问题描述】:

仪器指向这条线,说这里有泄漏

- (void) loadFavoriteData {
    TradePortMobileAppDelegate *delegate = [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context = [delegate managedObjectContext];

    NSManagedObjectModel *objectModel = [[context persistentStoreCoordinator] managedObjectModel];

    Session *session = delegate.session;

    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys: [session objectForKey:@"CTY_CODE"], pCOUNTRY, nil];

    NSFetchRequest *fetchRequest = [objectModel fetchRequestFromTemplateWithName:@"fetchAllFavorites" substitutionVariables:param];

    [fetchRequest setSortDescriptors: self.dataSorter];
    NSError *error;

    //THIS IS THE LINE INSTRUMENT SAYS THERE IS A LEAK!!!!!
    NSMutableArray *favorites = [[context executeFetchRequest:fetchRequest error:&error] mutableCopy];


    if ([favorites count] > 0) {

        [self.favoriteList removeAllObjects];


        for (NSInteger i=0; i < [favorites count]; i++) {
            FavoriteData *favorite = [favorites objectAtIndex: i];

            if (i < vMaxRecordsInCoreData) {

                [self.favoriteList addObject:[FavoriteInfo favoriteInfoWithClientId:favorite.clientId               withName:favorite.name 
                withAddress:favorite.address 
                withPhone:favorite.phone 

    withEmail:favorite.email 

    withCountry:favorite.country 

    withLtpId:favorite.ltpId
                withUpdateTimestamp:favorite.updateTimestamp
                withNoOfDetails:favorite.noOfDetails]];

    }

    else {

    [context deleteObject:favorite];

    }

        }

        if (![context save:&error]) {
            NSLog(@"deleting excess favorites failed.");
        }
        self.navigationItem.leftBarButtonItem.enabled = YES;

    }
    else {
        [self.favoriteList removeAllObjects];
        self.navigationItem.leftBarButtonItem.enabled = NO;
    }

    [favorites removeAllObjects];
    [favorites release];

}

这是栈

   0 CoreFoundation _CFRuntimeCreateInstance
   1 CoreFoundation __CFStringCreateImmutableFunnel3
   2 CoreFoundation CFStringCreateWithCString
   3 CoreData -[NSSQLCore _prepareResultsFromResultSet:usingFetchPlan:withMatchingRows:]
   4 CoreData -[NSSQLCore _newRowsForFetchPlan:selectedBy:withArgument:]
   5 CoreData -[NSSQLCore newRowsForFetchPlan:]
   6 CoreData -[NSSQLCore objectsForFetchRequest:inContext:]
   7 CoreData -[NSSQLCore executeRequest:withContext:error:]
   8 CoreData -[NSPersistentStoreCoordinator executeRequest:withContext:error:]
   9 CoreData -[NSManagedObjectContext executeFetchRequest:error:]
  MY CODE HERE--->10 TradePortMobile -[FavoritesTableViewController loadFavoriteData] /Users/aldrich/Projects/iPhone/Classes/FavoritesTableViewController.m:281
  11 TradePortMobile -[FavoritesTableViewController viewWillAppear:] /Users/aldrich/Projects/iPhone/Classes/FavoritesTableViewController.m:54
  12 UIKit -[UINavigationController viewWillAppear:]
  13 UIKit -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:]
  14 UIKit -[UITabBarController transitionFromViewController:toViewController:]
  15 UIKit -[UITabBarController _setSelectedViewController:]
  16 UIKit -[UITabBarController _tabBarItemClicked:]
  17 UIKit -[UIApplication sendAction:to:from:forEvent:]
  18 UIKit -[UITabBar _sendAction:withEvent:]
  19 UIKit -[UIApplication sendAction:to:from:forEvent:]
  20 UIKit -[UIControl sendAction:to:forEvent:]
  21 UIKit -[UIControl(Internal) _sendActionsForEvents:withEvent:]
  22 UIKit -[UIControl sendActionsForControlEvents:]
  23 UIKit -[UIApplication sendAction:to:from:forEvent:]
  24 UIKit -[UIControl sendAction:to:forEvent:]
  25 UIKit -[UIControl(Internal) _sendActionsForEvents:withEvent:]
  26 UIKit -[UIControl touchesEnded:withEvent:]
  27 UIKit -[UIWindow _sendTouchesForEvent:]
  28 UIKit -[UIApplication sendEvent:]
  29 UIKit _UIApplicationHandleEvent
  30 GraphicsServices PurpleEventCallback
  31 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__
  32 CoreFoundation __CFRunLoopDoSource1
  33 CoreFoundation __CFRunLoopRun
  34 CoreFoundation CFRunLoopRunSpecific
  35 CoreFoundation CFRunLoopRunInMode
  36 GraphicsServices GSEventRunModal
  37 GraphicsServices GSEventRun
  38 UIKit UIApplicationMain
  39 TradePortMobile main /Users/aldrich/Projects/iPhone/main.m:14
  40 TradePortMobile start

有人可以推动我朝着正确的方向前进吗?或者我无法解决这个泄漏的答案:)

【问题讨论】:

    标签: iphone objective-c memory-leaks nsmutablearray


    【解决方案1】:

    泄漏工具只能指向已分配内存的位置,泄漏可能发生在您在以下代码中从数组中传递每个对象的一些属性时:

    [self.favoriteList addObject:[FavoriteInfo favoriteInfoWithClientId:favorite.clientId               withName:favorite.name 
                    withAddress:favorite.address 
                    withPhone:favorite.phone 
    
        withEmail:favorite.email 
    
        withCountry:favorite.country 
    
        withLtpId:favorite.ltpId
                    withUpdateTimestamp:favorite.updateTimestamp
                    withNoOfDetails:favorite.noOfDetails]];
    

    您需要检查 favoriteInfoWithClientId:favorite 的参数会发生什么 - 如果您保留其中任何一个,那么您将有泄漏...

    希望对你有帮助

    【讨论】:

    • 谢谢,我会调查的
    • dealloc 中有一个 removeAllObjects 和 release for favoriteList。这应该覆盖它吗?我怎样才能让 favoriteList 的内容不被泄露?
    • removeAllObjects on favoriteList “应该”覆盖它,但这取决于很多事情!例如,如果 FavoriteInfo 类在它的 dealloc 中没有清理,那么你就会有泄漏。
    猜你喜欢
    • 2021-11-19
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 2011-12-17
    • 2018-07-23
    • 1970-01-01
    相关资源
    最近更新 更多