gpt4 book ai didi

iOS - cxx_destruct 中的 EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-12-02 14:01:52 24 4
gpt4 key购买 nike

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

最佳答案

我认为你的问题在这两行代码__weak typeof(self)weakSelf = self;和__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 进行弱引用

关于iOS - cxx_destruct 中的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38842247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com