I have the following object:
typedef void(^Block1)();
typedef void(^Block2)(Block1 block1);
@interface Test : NSObject
@end
@implementation Test
-(void)test:(Block2)block2 {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
Block1 block1 = ^{
// intentionally hold a ref of current object
[self doNothing];
};
@try {
block2(block1);
} @catch (NSException *exception) {
// do nothing
}
});
}
-(void)doNothing { }
-(void)dealloc{
NSLog(@"deallocated: %@", self);
}
@end
And I use it like the following:
int main(int argc, const char * argv[]) {
@autoreleasepool {
Test *t = [[Test alloc] init];
[t test:^(Block1 block1) {
// this @throw statement causes leak?
@throw [NSException exceptionWithName:@"ERROR" reason:nil userInfo:nil];
}];
}
return 0;
}
If I @throw exception from Block2
(As I did above), -dealloc
method will never be called, If I do anything other than @throwing exceptions, everything works as expected.
Did I do anything wrong in the code or is this a bug?