zoukankan      html  css  js  c++  java
  • NSInvocation 返回值在ARC下面的释放问题

    一、先看下面的代码

    -(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;
    }
    

      下面的代码会crash,Xcode提示的信息是 -[NSArray release] NSArray send release to deleacted object, 也就是NSArray对象重复释放了

      在ARC下面,一个方法返回的是autorelease对象的时候,通过NSInvocaton作为返回值的时候,注意调用的时候的方法,如果按照上面的方法

      resultSet默认是strong的,在这个方法底部,编译器会插入resultSet release的代码,同时getReturnValue 取的是指针的地址,将这个地址填充指向返回autorelease对象的地址,最后造成对象释放两次

      如果需要使用正确的结果,可以采用下面的方式

      

        或者同步的使用resultSet, 但是使用完成之后直接设置成nil

  • 相关阅读:
    Linux的学习--系统目录
    PHP内核的学习--创建PHP扩展
    PHP的学习--连接MySQL的三种方式
    MIME Type
    颜色的命名
    JavaScript的学习--生成二维码
    MySQL的学习--触发器
    Google Guava之--cache
    java代码调用oracle存储过程
    oracle序列
  • 原文地址:https://www.cnblogs.com/doudouyoutang/p/10304191.html
Copyright © 2011-2022 走看看