2011-12-05 196 views
0

我得到了helper.offlineQueue行的分配一个NSOperationQueue对象的泄漏。问题是,我不太确定在哪里发布它在这种方法...如何解决此泄漏问题?

+ (void)flushOfflineQueue 
{ 
    // TODO - if an item fails, after all items are shared, it should present a summary view and allow them to see which items failed/succeeded 

    // Check for a connection 
    if (![self connected]) 
     return; 

    // Open list 
    NSMutableArray *queueList = [self getOfflineQueueList]; 

    // Run through each item in the quietly in the background 
    // TODO - Is this the best behavior? Instead, should the user confirm sending these again? Maybe only if it has been X days since they were saved? 
    //  - want to avoid a user being suprised by a post to Twitter if that happens long after they forgot they even shared it. 
    if (queueList != nil) 
    { 
     SHK *helper = [self currentHelper]; 

     if (helper.offlineQueue == nil) 
      helper.offlineQueue = [[NSOperationQueue alloc] init];  

     SHKItem *item; 
     NSString *sharerId, *uid; 

     for (NSDictionary *entry in queueList) 
     { 
      item = [SHKItem itemFromDictionary:[entry objectForKey:@"item"]]; 
      sharerId = [entry objectForKey:@"sharer"]; 
      uid = [entry objectForKey:@"uid"]; 

      if (item != nil && sharerId != nil) 
       [helper.offlineQueue addOperation:[[[SHKOfflineSharer alloc] initWithItem:item forSharer:sharerId uid:uid] autorelease]]; 
     } 

     // Remove offline queue - TODO: only do this if everything was successful? 
     [[NSFileManager defaultManager] removeItemAtPath:[self offlineQueueListPath] error:nil]; 

    } 
} 

谢谢!

回答

1

我认为你应该只是做:

helper.offlineQueue = [[[NSOperationQueue alloc] init] autorelease]; 

新鸿基对象本身应保留队列会释放它,当它完成。由于alloc而引起的引用可以立即释放。