NSOperationQueue泄漏?

时间:2011-12-28 19:17:14

标签: ios multithreading nsoperation

我正在尝试使用NSOperationQueue在后台线程中执行一个方法,如下所示:

NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                            selector:@selector(method)
                                                                              object:nil];

    [queue addOperation:operation];
    [queue release];
    [operation release];

问题是,分析师说存在泄漏存储在队列中。

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

调用[MyClass new]与调用[[MyClass alloc] init]相同,它返回带有retainCount = 1的对象。 所以,它应该在之后发布。

答案 1 :(得分:1)

您是否正在发布operation个对象?尝试添加autorelease关键字

    NSInvocationOperation *operation = [[[NSInvocationOperation alloc] initWithTarget:self
                                                                                selector:@selector(method)
                                                                                  object:nil] autorelease];

答案 2 :(得分:1)

只是想知道,你在方法“方法”中做了什么?你在使用NSAutoreleasePool吗?顺便说一下,使用this回答来帮助你。