【问题标题】:Objective-C EXC-BAD-ACCESS - resolving without ARC?Objective-C EXC-BAD-ACCESS - 没有ARC解决?
【发布时间】:2020-04-27 14:23:27
【问题描述】:

作为参考,我使用的是 Xcode 11.3

我遇到了一个已释放对象的问题,它导致 EXC BAD ACCESS。 好消息是我确切地知道对象是什么。 我不知道如何解决它。

这是发生崩溃的代码...

- (void)didSendPTPCommand:(NSData*)command inData:(NSData*)data response:(NSData*)response error:(NSError*)error contextInfo:(void*)contextInfo
{
    NSLog(@"%@ %@ %@ %@", NSStringFromSelector(_cmd), data, response, error);

    PTPOperationRequest*  ptpRequest  = (__bridge PTPOperationRequest*)contextInfo;
    PTPOperationResponse* ptpResponse = NULL;

    if ( ptpRequest )

崩溃开始了:

PTPOperationRequest*  ptpRequest  = (__bridge PTPOperationRequest*)contextInfo;

这段代码是从这段代码中调用出来的:

            ptpData       = NULL;
            PTPOperationRequest*  request       = [[PTPOperationRequest alloc] init];
            request.operationCode       = PTPOperationCodeInitiateCapture;
            request.numberOfParameters  = 0;
            commandBuffer               = request.commandBuffer;

            [camera requestSendPTPCommand:commandBuffer
                                  outData:NULL
                      sendCommandDelegate:self
                   didSendCommandSelector:@selector(didSendPTPCommand:inData:response:error:contextInfo:)
                              contextInfo:(__bridge void * _Nullable)(request)];

我试图传递“请求”的诅咒在哪里。 很久以前,我会通过保留/释放来管理这个 - 不再是。我现在该怎么办?

  • 大卫

【问题讨论】:

    标签: objective-c automatic-ref-counting exc-bad-access


    【解决方案1】:

    我想我现在有了解决办法。

    __bridge_retained
    

    所以下面的代码就这样改了:

        [camera requestSendPTPCommand:commandBuffer
                              outData:NULL
                  sendCommandDelegate:self
               didSendCommandSelector:@selector(didSendPTPCommand:inData:response:error:contextInfo:)
                          contextInfo:(__bridge_retained void * _Nullable)(request)];
    

    我已经测试过了,它正在工作。

    • 大卫

    【讨论】:

    • 您应该考虑那里发生的内存泄漏:请求被保留并且不会在任何地方释放。 requestSendPTPCommand:commandBuffer 是什么?也许您可以使用块而不是带有上下文信息的选择器将其更改为更现代的方式?
    • 我最终使用了 CFBridgingRetain 和 CFBridgingRelease。这些用于传入的contextInfo,并在didSendPTPCommand:inData:response:error:contextInfo:中发布-我认为解决方案是整洁和可控的。
    • 比以前好多了,但是如果永远不会调用didSendPTPCommand:inData:response:error:contextInfo呢?
    • 同意这可能是个问题。虽然我认为不会出现不调用的情况。这意味着它在成功或失败时被调用。文档没有确认或否认。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多