OGeek|极客世界-中国程序员成长平台

标题: ios - 将捕获 block 传递给 DispatchQueue 时出现段错误 11 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:38
标题: ios - 将捕获 block 传递给 DispatchQueue 时出现段错误 11

在 DispatchQueue 的捕获 block 上,我在 Swift 4 和 Swift 4.2 上都遇到了段错误。

我试图为 DispatchQueue 的转义参数提供一个捕获 block ,以保证传递给被调用闭包的对象在被调用时是事件的。

代码如下所示:

import Foundation

struct Response {
    let outcome: String?
}

enum NotEvenError: Error {
    case notEven
}

struct IsEvenService {
    typealias SuccessCallback = (Response) -> Void
    typealias FailureCallback = (Error?) -> Void

    func perform(success: @escaping SuccessCallback, failure: @escaping FailureCallback) {
        let number = Int.random(in: 0 ... 10)
        if number%2 == 0 {
            let outcome = Response(outcome: "it is")
            success(outcome)
        } else {
            failure(NotEvenError.notEven)
        }
    }
}

func runTest(success: @escaping (Response) -> Void, failure: @escaping (Error?) -> Void) {
    let service = IsEvenService()
    service.perform(success: { (response) in
        if response.outcome != nil {
            DispatchQueue.main.async { [resp = response] in
                success(resp)
            }
        }
    },
                    failure: { (error) in
        DispatchQueue.main.async { [err = error] in
            failure(err)
        }
    })
}

有问题的部分如下:

DispatchQueue.main.async { [err = error] in
    failure(err)
}

这是一个示例文件,可以通过命令行中的 swiftc 调用轻松验证。

结果如下:

Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1)
Target: x86_64-apple-darwin17.7.0
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file test.swift -target x86_64-apple-darwin17.7.0 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -color-diagnostics -module-name test -o /var/folders/fn/wh62twgj54180b7x1dx9j6080000gp/T/test-d0f7f4.o
0  swift                    0x000000010720964a PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x0000000107208dfe SignalHandler(int) + 302
2  libsystem_platform.dylib 0x00007fff7ac3ff5a _sigtramp + 26
3  libsystem_platform.dylib 0x00007ffeec7e0000 _sigtramp + 1908015296
4  swift                    0x0000000103eef293 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 6531
5  swift                    0x0000000103ea826e swift:owering::SILGenFunction::emitFunction(swift::FuncDecl*) + 462
6  swift                    0x0000000103e0be14 swift:owering::SILGenModule::emitFunction(swift::FuncDecl*):_1:perator()(swift::SILFunction*) const + 516
7  swift                    0x0000000103e0b142 swift:owering::SILGenModule::emitFunction(swift::FuncDecl*) + 1042
8  swift                    0x0000000103e1501b swift:owering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 939
9  swift                    0x0000000103e16bd5 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool) + 1333
10 swift                    0x00000001034983fe performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 28990
11 swift                    0x000000010348ddc5 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7717
12 swift                    0x0000000103433a35 main + 1349
13 libdyld.dylib            0x00007fff7a931015 start + 1
14 libdyld.dylib            0x000000000000000f start + 2238509051
Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file test.swift -target x86_64-apple-darwin17.7.0 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -color-diagnostics -module-name test -o /var/folders/fn/wh62twgj54180b7x1dx9j6080000gp/T/test-d0f7f4.o 
1.  While emitting SIL for 'runTest(success:failure' at test.swift:26:1
2.  While silgen emitFunction SIL function "@$S4test7runTest7success7failureyyAA8ResponseVc_ys5Error_pSgctF".
 for 'runTest(success:failure' at test.swift:26:1
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: compile command failed due to signal 11 (use -v to see invocation)

是否可以假设如果我不传递捕获 block ,则在执行闭包时对象将被保留?

如果我执行以下操作,它将编译:

let uselessTempError = error
DispatchQueue.main.async { [err = uselessTempError] in
    failure(err)
}



Best Answer-推荐答案


您不需要 [err = error] 部分。以下 block 将保持对错误的强引用:

DispatchQueue.main.async {
    failure(error)
}

关于ios - 将捕获 block 传递给 DispatchQueue 时出现段错误 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52515459/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4