iOS - cxx_destruct中的EXC_BAD_ACCESS

时间:2016-08-09 04:44:11

标签: ios objective-c crash

我有一个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

1 个答案:

答案 0 :(得分:0)

我认为您在这两行代码<div ng-show="CaptionList.length < 1 || CaptionList.length > 3">Not Error Message</div> __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 }]; } 抓取自我,超出范围后会销毁。在这种情况下,不需要对自我进行弱引用