I'm trying to pass block arguments to a NSInvocation
, but the app crashes. The invocation makes a network request and calls the success or failure blocks. I think the problem is that blocks are dealloced before the network request finishes. I managed to get it to work with some Block_copy
hackery and it doesn't report any leaks using Instruments.
Questions: - Is it possible that the leak is there even though the Static Analyzer or Instruments is not reporting it? - Is there a better way to "retain" the block?
// Create the NSInvocation
NSMethodSignature *methodSignature = [target methodSignatureForSelector:selector];
NSInvocation* invoc = [NSInvocation invocationWithMethodSignature:methodSignature];
[invoc setTarget:target];
[invoc setSelector:selector];
// Create success and error blocks.
void (^successBlock)(id successResponse) = ^(id successResponse) {
// Some success code here ...
};
void (^errorBlock)(NSError *error) = ^(NSError *error) {
// Some failure code here ...
};
/*
Without the two Block_copy lines, the block gets dealloced too soon
and the app crashes with EXC_BAD_ACCESS
I tried [successBlock copy] and [failureBlock copy] instead,
but the app still crashes.
It seems like Block_copy is the only way to move the block to the heap in this case.
*/
Block_copy((__bridge void *)successBlock);
Block_copy((__bridge void *)errorBlock);
// Set the success and failure blocks.
[invoc setArgument:&successBlock atIndex:2];
[invoc setArgument:&errorBlock atIndex:3];
[invoc retainArguments]; // does not retain blocks
// Invoke the method.
[invoc invoke];
Update: I updated the code to below. The blocks are NSMallocBlocks
, but the app still crashes.
// Create success and error blocks.
int i = 0;
void (^successBlock)(id successResponse) = ^(id successResponse) {
NSLog(@"i = %i", i);
// Some success code here ...
};
void (^errorBlock)(NSError *error) = ^(NSError *error) {
NSLog(@"i = %i", i);
// Some failure code here ...
};
/*** Both blocks are NSMallocBlocks here ***/
// Set the success and failure blocks.
void (^successBlockCopy)(id successResponse) = [successBlock copy];
void (^errorBlockCopy)(NSError *error) = [errorBlock copy];
/*** Both blocks are still NSMallocBlocks here - I think copy is a NoOp ***/
// Set the success and failure blocks.
[invoc setArgument:&successBlockCopy atIndex:2];
[invoc setArgument:&errorBlockCopy atIndex:3];
[invoc retainArguments]; // does not retain blocks
// Invoke the method.
[invoc invoke];
The blocks are passed down in the chain as follows:
NSInvocation
→ NSProxy
(NSInvocation
using forwardInvocation:
) → method1
→ methodN
methodN
eventually calls the success or failure block depending on the HTTP response.
Do I need to copy the block at every stage? The example above was talking about the first NSInvocation
. Do I also need [invocation retainArguments];
at every appropriate step? I'm using ARC.