如何解决这个漏洞?

时间:2011-12-05 01:29:51

标签: ios memory-leaks nsoperationqueue sharekit

我在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 个答案:

答案 0 :(得分:1)

我希望你能做到:

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

SHK对象本身应该保留队列,并在完成后释放它。由于alloc而你持有的引用可以立即释放。