gpt4 book ai didi

ios - GCD/NSOperationQueue EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-11-29 12:33:03 29 4
gpt4 key购买 nike

我正在使用 GCD/NSOperationQueue 进行异步删除。

我实现了以下代码:

- (void)deleteWithCompletionHandler:(Handler)completionHandler
{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[queue addOperationWithBlock:^{

NSFileManager *fileManager = [NSFileManager defaultManager];

NSError *error;

if ([fileManager fileExistsAtPath:self.path]) {

BOOL success = [fileManager removeItemAtPath:self.path error:&error];

}

NSOperationQueue *main = [NSOperationQueue mainQueue];

[[NSOperationQueue mainQueue] addOperationWithBlock:^{

if (completionHandler) {

completionHandler(error, nil);
}
}];
}];
}

以及以下内容:

- (void)deleteWithCompletionHandler:(Handler)completionHandler
{
dispatch_queue_t queue = dispatch_queue_create("My Queue", NULL);

dispatch_async(queue, ^{

NSFileManager *fileManager = [NSFileManager defaultManager];

NSError *error;

if ([fileManager fileExistsAtPath:self.path]) {

BOOL success = [fileManager removeItemAtPath:self.path error:&error];

}


dispatch_async(dispatch_get_main_queue(), ^{

if (completionHandler) {

completionHandler(error, nil);
}
});
});
}

这两个代码片段都会在返回主线程时导致 EXC_BAD_ACCESS。

我做错了什么?项目中部分代码没有使用ARC。

谢谢。

最佳答案

由于您没有使用 ARC,因此您的堆栈变量未初始化为 0/nil。因此 completionHandler(error, nil); 将使用随机堆栈垃圾调用 error

这是在查看您的代码时突出的一个问题,clang 静态分析器应该很容易捕捉到它。我建议您对代码运行分析器并查看所有警告。

可能还存在其他问题,因此如果您需要更多帮助,请提供您的崩溃报告。

此外,您的队列变量超出范围,因此您要么泄漏它(在 MRR 情况下),要么在仍在使用时释放它(在 ARC 情况下)。您可能应该只使用全局并发队列。

关于ios - GCD/NSOperationQueue EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27109893/

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