NSInvocation crash
来源:互联网 发布:天猫与淘宝的盈利方式 编辑:程序博客网 时间:2024/05/21 10:09
在使用NSInvocation的过程中,遇到一个问题。那就是在获取返回结果后,app很快crash。示例如下:
- 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;
- [invocation invoke];
- [invocation getReturnValue:&resultSet];
- return resultSet;
在设置Enable Zombies后发现,是由于系统多次释放NSArray * resultSet造成的非法内存访问。
原因是在arc模式下,getReturnValue:仅仅是从invocation的返回值拷贝到指定的内存地址,如果返回值是一个NSObject对象的话,是没有处理起内存管理的。而我们在定义resultSet时使用的是__strong类型的指针对象,arc就会假设该内存块已被retain(实际没有),当resultSet出了定义域释放时,导致该crash。假如在定义之前有赋值的话,还会造成内存泄露的问题。
解决办法:
使用一个unretain的对象来获取返回值,或者 用void *指针来保存返回值,然后用__bridge来转化为OC对象。
- NSArray * __unsafe_unretained tempResultSet;
- [invocation getReturnValue:&tempResultSet];
- NSArray *resultSet = tempResultSet;
或者
- void *tempResultSet;
- [invocation getReturnValue:&tempResultSet];
- NSArray *resultSet = (__bridge NSArray *)tempResultSet;
0 0
- NSInvocation crash
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation在获取返回值后crash问题
- NSInvocation在获取返回值后crash问题
- NSInvocation在获取返回值后crash问题
- NSInvocation在获取返回值后crash问题
- An NSInvocation
- NSInvocation用法
- NSInvocation使用
- NSInvocation 学习
- iOS NSInvocation
- Linux驱动开发———平台总线设备驱动
- Python Numpy Tutorials: 数组--3
- Recaman's Sequence
- sendmessage和getcomponents
- 欢迎使用CSDN-markdown编辑器
- NSInvocation crash
- Hadoop-2.6.5集群安装配置
- TI am335xD用到的数据手册及板级文件
- virtualbox centos7 minimal 开发环境安装,网络配置与宿主机共享文件夹设置
- 一键部署openstack
- linux命令之ulimit
- 常用的adb命令使用简介
- springboot调用微信的jscode2session报JSONObject异常
- Multi-Cloud模式下Terraform和Packer开源工具实践分享