我最近被“分配”到一段代码,其中以下标记为#warning memory leak
- (void)aMethodWithCallback:(BlockType)completedBlock {
/* Do stuff */
completedBlock = ^(Class1 *obj, NSError *error) {
/* Do stuff */
#warning memory leak
if (completedBlock) {
completedBlock(obj, error);
}
};
[self callAnotherMethodWithCallback::completedBlock];
}
我不确定如何处理它,创建一个名称为输入块的块看起来很奇怪,但它似乎有效。为什么上面的代码很危险?
编辑 ARC 已启用。
将代码更改为此,它似乎不再泄漏:
- (void)aMethodWithCallback:(BlockType)completedBlock {
/* Do stuff */
BlockType completedBlock2 = ^(Class1 *obj, NSError *error) {
/* Do stuff */
if (completedBlock) {
completedBlock(obj, error);
}
};
[self callAnotherMethodWithCallback::completedBlock2];
}