在ARC中,dealloc方法调用方法/块包含对self的弱引用,导致weakSelf = nil

时间:2014-12-09 12:48:01

标签: ios objective-c automatic-ref-counting

正如标题所述,在ARC模式下,当我使用自引用定义块时(弱以避免引用周期):

...
id __weak weakSelf = self;
self.someBlock = ^(){
    if (weakSelf != nil){
        (do something with weakSelf...)
        return NO;
    }
    return YES;
};
...

在dealloc中我调用块:

- (void)dealloc {
    ...
    BOOL isNil = self.someBlock();
    ...
}

然后我发现isNil = YES。

当我想让dealloc中的块与self做某事时,这似乎是一个问题。

这也发生在回调函数中,我不会在这里给出一个例子。

我的解决方案是使用__unsafe_unretained而不是__weak。

但这看起来很难看。

是否有更好的方法可以避免dealloc中的自我弱?

更新了问题:即使weakSelf没有,为什么self会被识别?这是dealloc的原因吗?


为清楚起见,我粘贴下面的测试代码:

#import <Foundation/Foundation.h>

@interface blockWeak : NSObject

@property (nonatomic, strong) BOOL (^someBlock)();
@property (nonatomic) int num;

- (void)makeBlock;

@end

@implementation blockWeak

- (void)makeBlock
{
    typeof(self) __weak weakSelf = self;
    self.someBlock = ^(){
        typeof(self) strongSelf = weakSelf;
        strongSelf.num = 2;
        return weakSelf == nil?YES:NO;
    };
}

- (void)dealloc
{
    BOOL isNil = self.someBlock();
    if (isNil) {
        NSLog(@"weakSelf is nil.");
    }
}

@end

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        blockWeak *bw = [[blockWeak alloc] init];
        bw.num = 1;
        [bw makeBlock];
    }
    return 0;
}

2 个答案:

答案 0 :(得分:1)

你不要打电话给那个街区。那将是:

BOOL isNil = self.someBlock();

答案 1 :(得分:1)

以下是有效的,在我看来更清晰,因为暂不提及self。 此外,现在没有真正需要返回块BOOL,但我已将其留在...

#import <Foundation/Foundation.h>

@interface blockWeak : NSObject

@property (nonatomic, copy) BOOL (^someBlock)(blockWeak *);
@property (nonatomic) int num;

- (void)makeBlock;

@end

@implementation blockWeak

- (void)makeBlock
{
    self.someBlock = ^BOOL(blockWeak *object){
        object.num = 2;
        return object == nil?YES:NO;
    };
}

- (void)dealloc
{
    NSLog(@"num = %d", _num);
    BOOL isNil = _someBlock(self);
    if (isNil) {
        NSLog(@"block returned NO");
    } else {
        NSLog(@"block returned YES");
    }
    NSLog(@"num = %d", _num);
}

@end

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        blockWeak *bw = [[blockWeak alloc] init];
        bw.num = 1;
        [bw makeBlock];
    }
    return 0;
}

<强>输出

num = 1
block returned YES
num = 2