简体   繁体   English

使用Typhoon在运行时注入闭包时的EXC_BAD_ACCESS

[英]EXC_BAD_ACCESS when injecting a closure at runtime using Typhoon

I have a mixed ObjC and Swift iOS project. 我有一个混合的ObjC和Swift iOS项目。

I have a class for testing the injection of a trivial closure at runtime: 我有一个类用于测试在运行时注入一个简单的闭包:

@objc
class TyphoonClosureTester: NSObject {
    @objc var closure: (() -> Void)?

    @objc
    override init() {}

    @objc
    init(closure: (() -> Void)?) {
        self.closure = closure
    }

    @objc
    func callClosure() {
        guard let closure = closure else {
            assert(false, "no closure 1")
            return
        }
        closure()
        NSLog("Have called closure 1 OK")
    }
}

My assembly file for Typhoon contains this function: 我的Typhoon程序集文件包含这个函数:

func testAClosure(closure: @escaping () -> Void) -> AnyObject {
    return TyphoonDefinition.withClass(TyphoonClosureTester.self) { definition in
        definition?.useInitializer(#selector(TyphoonClosureTester.init(closure:))) { initializer in
            initializer?.injectParameter(with: closure)
        }

        definition?.scope = .prototype
    } as AnyObject
}

And I try using this closure as follows: 我尝试使用此闭包如下:

let closureTester1: TyphoonClosureTester = assembler.testAClosure(closure: {
    NSLog(" closure 1 called!")
}) as! TyphoonClosureTester

// causes EXC_BAD_ACCESS
closureTester1.callClosure()

but this results in EXC_BAD_ACCESS when I call the closure. 但是当我调用闭包时,这会导致EXC_BAD_ACCESS (Exact message is Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) ) So it looks like the closure is being deallocated somewhere. (确切的消息是Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) )所以它看起来像是在某处解除分配。

I've tried adding capture into the assembly (ie [closure] ) but it makes no difference: 我已经尝试将捕获添加到程序集中(即[closure] )但它没有区别:

func testAClosure(closure: @escaping () -> Void) -> AnyObject {
    return TyphoonDefinition.withClass(TyphoonClosureTester.self) { [closure] definition in
        definition?.useInitializer(#selector(TyphoonClosureTester.init(closure:))) { [closure] initializer in
            initializer?.injectParameter(with: closure)
        }

        definition?.scope = .prototype
    } as AnyObject
}

I also tried injecting the closure as a property, instead of via init, and it made no difference. 我也尝试将闭包作为属性注入,而不是通过init,它没有任何区别。

From memory this is not supported. 从内存中不支持。 You may try looking in the test cases for an example. 您可以尝试查看测试用例中的示例。

Injecting wrapped primitives is described here: https://github.com/appsquickly/typhoon/wiki/wrap-primitive-values-into-NSValue 这里描述了注入包裹的基元: https//github.com/appsquickly/typhoon/wiki/wrap-primitive-values-into-NSValue

. . however there's no reference to blocks/closures. 但是没有提到块/闭包。

If you describe what you would like to achieve, a next-best workaround could be suggested. 如果您描述了您希望实现的目标,可以建议采用下一个最佳解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM