NSInvocation返回值但使EXC_BAD_ACCESS崩溃

sly*_*sid 21 objective-c nsinvocation ios

我有一个数组,我正在迭代并寻找一个特定的标志.如果标志值为nil,我正在调用一个生成调用对象并返回调用结果的方法.

我的代码结构如下

for(NSString *key in [taxiPlanes allKeys])
{
        Plane *currentPlane = [taxiPlanes objectForKey:key];

        if(currentPlane.currentAction == nil)
        {
            NSString *selector = [[currentPlane planeTakeoffSequence] firstObject];
            currentPlane.currentAction = selector;

            // Calling for NSInvocation in [self ...]
            NSArray *action = [NSArray arrayWithArray:[self operationFromTakeoffAction:currentPlane.currentAction AtPoint:currentPlane.position]];

        NSLog(@"%@",action);
        }
 }
Run Code Online (Sandbox Code Playgroud)

生成NSInvocation的方法

-(NSArray *) operationFromTakeoffAction:(NSString *) action AtPoint:(CGPoint) flightPoint
{
    NSMethodSignature *methodSignature = [FlightOperations instanceMethodSignatureForSelector:NSSelectorFromString(action)];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];

    [invocation setTarget:fOps];
    [invocation setSelector:NSSelectorFromString(action)];
    [invocation setArgument:&flightPoint atIndex:2];

    NSArray *resultSet = [NSArray alloc]init];
    [invocation invoke];
    [invocation getReturnValue:&resultSet];

    return resultSet;
}
Run Code Online (Sandbox Code Playgroud)

在for循环中,没有NSInvocation([self ....])的方法调用,循环只执行正常而不会崩溃.但是,当我介绍调用NSInvocation的方法时,我能够看到NSLog in for循环打印出预期的NSArray结果,但它崩溃了错误消息EXC_BAD_ACCESS.

即使NSInvocation返回正确的结果,我也无法弄清楚为什么它会失败.没有NSInvocation,for循环不会崩溃.

任何的意见都将会有帮助.

谢谢

new*_*cct 76

我猜你在使用ARC?

问题在于线路[invocation getReturnValue:&resultSet];.getReturnValue:只需将返回值的字节复制到给定的内存缓冲区中,无论类型如何.如果返回类型是可保留的对象指针类型,则它不知道或不关心内存管理.由于resultSet__strong对象指针类型的变量,ARC假定已保留已放入变量的任何值,因此当它超出范围时将释放它.在这种情况下不是这样,所以它崩溃了.(同样,你有数组resultSet最初点会被泄露,因为getReturnValue:覆盖该值不释放它.你为什么还送变量点摆在首位的目标是超越我.)

解决方案是你必须指向一个非保留类型的指针getReturnValue:.或者:

NSArray * __unsafe_unretained tempResultSet;
[invocation getReturnValue:&tempResultSet];
NSArray *resultSet = tempResultSet;
Run Code Online (Sandbox Code Playgroud)

要么:

void *tempResultSet;
[invocation getReturnValue:&tempResultSet];
NSArray *resultSet = (__bridge NSArray *)tempResultSet;
Run Code Online (Sandbox Code Playgroud)

  • 巨大的拥抱和亲吻. (4认同)
  • 我有同样的问题,这个解决方案就像一个魅力!:) (3认同)