【问题标题】:iOS - EXC_BAD_ACCESS in cxx_destructiOS - cxx_destruct 中的 EXC_BAD_ACCESS
【发布时间】:2016-08-09 13:10:09
【问题描述】:

我有一个 NSOperationQueue,maxConcurrentOperationCount = 1。

所有操作都添加到 NSOperationQueue addOperationWithBlock:.

现在发生了低频崩溃,调用栈如下。

 Crashed: NSOperationQueue :: NSOperation 0x14fbf2d20 (QOS: LEGACY)
0  libobjc.A.dylib                0x1811a8160 objc_release + 16
1  MyApp                          0x1015437f0 -[MyClass .cxx_destruct] (MyClass.m:23)
2  libobjc.A.dylib                0x18118eb54 object_cxxDestructFromClass(objc_object*, objc_class*) + 148
3  libobjc.A.dylib                0x18119a040 objc_destructInstance + 92
4  libobjc.A.dylib                0x18119a0a0 object_dispose + 28
5  MyApp                          0x101546aec __destroy_helper_block_ (MyNSOperationQueue.m)
6  libsystem_blocks.dylib         0x1815d18e8 _Block_release + 156
7  Foundation                     0x182437f8c -[NSBlockOperation dealloc] + 64
8  Foundation                     0x1824e6a18 __NSOQSchedule_f + 452
9  libdispatch.dylib              0x18157547c _dispatch_client_callout + 16
10 libdispatch.dylib              0x1815814c0 _dispatch_queue_drain + 864
11 libdispatch.dylib              0x181578f80 _dispatch_queue_invoke + 464
12 libdispatch.dylib              0x181583390 _dispatch_root_queue_drain + 728
13 libdispatch.dylib              0x1815830b0 _dispatch_worker_thread3 + 112
14 libsystem_pthread.dylib        0x18178d470 _pthread_wqthread + 1092
15 libsystem_pthread.dylib        0x18178d020 start_wqthread + 4

主要代码如下。

@interface MyNSOpetationQueue ()

@property (nonatomic, strong) NSOperationQueue *operationQueue;

@end

@implementation MyNSOpetationQueue

#pragma mark - init

+ (MyNSOpetationQueue *)sharedSender {
    static MyNSOpetationQueue *analyticsSender = nil;
    static dispatch_once_t pred;

    dispatch_once(&pred, ^{
        analyticsSender = [[MyNSOpetationQueue alloc] init];
    });

    return analyticsSender;
}

- (instancetype)init {
    if ((self = [super init])) {
        _operationQueue = [[NSOperationQueue alloc] init];
        [_operationQueue setMaxConcurrentOperationCount:1];
    }

    return self;
}

#pragma mark public method

- (void)addOneObj:(MyClass *)obj {
    if (!obj) {
        return;
    }

    __weak typeof(self) weakSelf = self;
    [self.operationQueue
     addOperationWithBlock:^{
         __strong typeof(weakSelf) strongSelf = weakSelf;
         //1.insert `obj` to Sqlite
         //2.do other thing
     }];
}

@end

【问题讨论】:

  • 能否提供你的代码,你是如何使用add和使用NSBlockOperation的?

标签: ios objective-c crash


【解决方案1】:

我认为你的问题在这两行代码__weak typeof(self) weakSelf = self;and __strong typeof(weakSelf) strongSelf = weakSelf;尝试删除。

- (void)addOneObj:(MyClass *)obj {
    if (!obj) {
        return;
    }

    [self.operationQueue addOperationWithBlock:^{
         //1.insert `obj` to Sqlite
         //2.do other thing
     }];
}

您的addOperationWithBlock 捕获了self,超出范围后将销毁。在这种情况下,不需要对 self 进行弱引用

【讨论】:

  • 这两行代码__weak typeof(self) weakSelf = self; and __strong typeof(weakSelf) strongSelf = weakSelf在我之前的代码中没有出现,但是也崩溃了。
  • addOperationWithBlock 中有什么代码? //1.insert obj 到 Sqlite 或 //2.do other thing 可能有问题
猜你喜欢
  • 1970-01-01
  • 2017-02-16
  • 2012-09-21
  • 1970-01-01
  • 2014-07-23
  • 2020-08-17
  • 2012-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多