我正在尝试将一个块作为参数发送到NSInvocation调用的方法(对于上下文,由NSInvocationOperation触发)。调用应该保留参数,它似乎适用于“常规”对象参数,但Block的retainCount保持在1。
我可以在方法调用中使用它后释放它,但如果队列在调用操作之前解散,理论上可能会泄漏它。
一些代码:
NSInvocationOperation *load = [[NSInvocationOperation alloc] initWithInvocation:loadInvoc];
NSAssert([loadInvoc argumentsRetained],@"Arguments have not been retained");
[loader release];
NSInvocation *completionInvoc = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(serviceCompletionBlock:afterInvocationCompleted:)]];
[completionInvoc setTarget:self];
[completionInvoc setSelector:@selector(serviceCompletionBlock:afterInvocationCompleted:)];
MFEImageCallback callback = [completionBlock copy];
[completionInvoc setArgument:&callback atIndex:2];
[completionInvoc setArgument:&load atIndex:3];
NSInvocationOperation *completion = [[NSInvocationOperation alloc] initWithInvocation:completionInvoc];
NSAssert([completionInvoc argumentsRetained],@"Completion handler not retaining");
[callback release];
[completion addDependency:load];
我正在使用的块(在NSManagedObject
子类的访问器方法中定义):
^(UIImage *image,NSError *err){
[self setValue:image forKey:key];
}
答案 0 :(得分:3)
没有看到你的块的内容,就不可能说了。如果你的块实际上是一个静态块,那么复制它什么也不做。
你看到了崩溃吗?