我有以下对象:
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
我使用它如下:
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;
}
如果我从Block2
@throw异常(正如我上面所做的那样),永远不会调用-dealloc
方法,如果除了@throwing异常之外我做任何其他事情,一切都按预期工作。
我在代码中做错了什么,或者这是一个错误?