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.
Block_copy
, and indeed [block copy]
return copies. They don't magically switch the original with a copy at the same location. So at the very least I think you want:
successBlock = Block_copy((__bridge void *)successBlock);
errorBlock = Block_copy((__bridge void *)errorBlock);
(or, equivalently, successBlock = [successBlock copy]; ...
)
Otherwise you're creating copies, doing nothing with them and still passing the originals off to the invocation.
EDIT: so, I put the following code into a project:
@interface DummyClass: NSObject
@end
typedef void (^ successBlock)(id successResponse);
typedef void (^ failureBlock)(NSError *error);
@implementation DummyClass
- (id)init
{
self = [super init];
if(self)
{
SEL selector = @selector(someMethodWithSuccess:failure:);
id target = self;
// 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 ...
NSLog(@"Off, off, off with %@", successResponse);
};
void (^errorBlock)(NSError *error) = ^(NSError *error) {
// Some failure code here ...
NSLog(@"Dance, dance, dance till %@", error);
};
successBlock = [successBlock copy];
errorBlock = [errorBlock copy];
// 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.
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(),
^{
[invoc invoke];
});
}
return self;
}
- (void)someMethodWithSuccess:(successBlock)successBlock failure:(failureBlock)failureBlock
{
NSLog(@"Words:");
successBlock(@[@"your", @"head"]);
failureBlock([NSError errorWithDomain:@"you're dead" code:0 userInfo:nil]);
}
@end
And added the following to the end of application:didFinishLaunchingWithOptions:
:
DummyClass *unusedInstance = [[DummyClass alloc] init];
The result is that two seconds after launching my program the following appears on the console:
2013-06-02 20:11:56.057 TestProject[3330:c07] Words:
2013-06-02 20:11:56.059 TestProject[3330:c07] Off, off, off with (
your,
head
)
2013-06-02 20:11:56.060 TestProject[3330:c07] Dance, dance, dance till Error Domain=you're dead Code=0 "The operation couldn’t be completed. (you're dead error 0.)"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With