NSInvocation 获取目标导致 EXC_BAD_ACCESS

NSInvocation get target causing EXC_BAD_ACCESS

我对 NSInvocation 有一个奇怪的问题。当网络操作完成时,我将它用作 return 回调。让我更详细地解释一下前面的句子:

我正在使用通过 TCP 套接字工作的定制网络协议,我有一个 class 使用此协议并用作与我的服务器的连接。现在 class 有一个方法可以说 performNetworkRequestWithDelegate: 是这样实现的:

- (void)performNetworkRequestWithDelegate:(id<MyClassDelegate>)delegate
{
    NSString *requestKey = [self randomUniqueString];
    id request = [self assembleRequestAndSoOnAndSoForth];
    [request setKey:requestKey];

    SEL method = @selector(callbackStatusCode:response:error:);
    NSMethodSignature *signature = [delegate methodSignatureForSelector:method];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    invocation.target = delegate;
    invocation.selector = method;

    delegateInvocationMap[requestKey] = invocation; //See below for an explanation what the delegateInvocationMap is

    [self sendRequest:request];
}

好的,我知道有些事情需要解释一下。首先,除了 requestKey 之外,不要理会与请求相关的任何事情。它是这样工作的:当我从服务器收到响应时,请求密钥会循环返回给我。所以它就像设置一个 HTTP header 字段,当您从服务器获得响应时,该字段会返回给您。这样我就可以确定发出了哪个请求。 delegateInvocationMap 是一个 NSMutableDictionary,它保留我们的调用,当我们得到响应并解析出 requestKey.

时,我们可以得到正确的调用

现在响应的处理程序是这样的:

- (void)processResponse:(id)response
{
    //Check for errors and whatnot

    NSString *requestKey = [response requestKey];
    if (!requestKey) return; //This never happens and is handled more correctly but keep it like this for the sake of simplicity

    NSInvocation *invocation = delegateInvocationMap[requestKey];
    if (!invocation) return; 

    [delegateInvocationMap removeObjectForKey:requestKey];

    if (!invocation.target) return; //THIS LINE IS THE PROBLEM

    [self setInvocationReturnParams:invocation fromResponse:response];
    [invocation invoke]; //This works when everything is fine
}

当响应成功return或出现任何错误我都正确处理时,此功能也有效。除了一个:

当调用的目标被解除分配时,我在尝试检查是否有我的调用目标时得到一个 EXC_BAD_ACCESS。苹果文档说:

The receiver’s target, or nil if the receiver has no target.

如何检查接收器是否已经被释放?这是一个巨大的痛苦。

编辑: 在下面的评论中,我发现访问已释放的 object 始终是未知行为。我不知道是否有任何官方文档对此进行说明(我还没有检查),但我有一个解决方法。是否可以通过 KVO 观察 dealloc 调用的调用目标?

NSInvocationtarget 属性 不是 ARC weak 引用;它被定义为 assign。如果您不持有对该对象的任何引用,它将被释放,您将开始看到 EXC_BAD_ACCESS 异常。

@property(assign) id target

ARC 自动将 assign 属性转换为 unsafe_unretained 而不是 weak。 A weak 属性 将在对象被释放时设置为 nilunsafe_unretained 属性 将继续指向内存地址,这将是垃圾。

您可以使用 retainArguments 方法解决此问题。

[invocation retainArguments];

来自文档:

If the receiver hasn’t already done so, retains the target and all object arguments of the receiver and copies all of its C-string arguments and blocks.

由于 NSInvocation 想要保留目标,但您本质上希望它保留弱引用,因此请使用 TPDWeakProxy 之类的东西。代理接受一个引用并用弱指针持有它,但代理可以强持有。

以下是我在 OCMockito 中的 NSInvocation 类别方法中的做法:

- (void)mkt_retainArgumentsWithWeakTarget
{
    if (self.argumentsRetained)
        return;
    TPDWeakProxy *proxy = [[TPDWeakProxy alloc] initWithObject:self.target];
    self.target = proxy;
    [self retainArguments];
}

这会将目标替换为本质上较弱的目标。