-3

嗨,当我尝试获取全局变量的值或设置值时,我遇到了问题。它在应用程序生命周期方法中的代码,applicationDidEnterBackground当我需要实时知道调用状态时,这对我有用,但有时会生成 EXC_BAD_ACCESS

这是变量声明@property(nonatomic, copy) NSNumber *calling;,在我的 .h 文件中。

这是我的 .m 实现文件的代码:

__unsafe_unretained typeof(self) weakSelf = self;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    @autoreleasepool
    {
        while (weakSelf.calling.intValue == 1)
        {
            callCenter.callEventHandler = ^(CTCall* call)
            {
                if([call.callState isEqualToString:CTCallStateDisconnected])
                {
                   weakSelf.calling = [[NSNumber numberWithInt:0] copy];//Error EXC_BAD_ACCESS

                }

            };
        }
    }
});

但有时会产生错误,有时不会。任何想法?

4

1 回答 1

4

Stop using this weakSelf. It is not necessary. self does not retain this block. There is no retain cycle.

When you say that __weak "does not work", it is a sign that self has already been deallocated by the time the block runs, so the weak reference has been set to nil, thus messages to it do nothing. This is also why __unsafe_unretained crashes -- self has already been deallocated; without __weak to set it to nil, it is a dangling pointer.

于 2013-04-17T19:00:35.780 回答