iOS开发使用 runtime 方法中的 class_copyIvarList,class_copyMethodList 方法时导致内存泄漏问题
2018-04-10 15:54
585 查看
前段时间在做公司新项目的时候使用了 runtime 的 class_copyIvarList 方法来获取类的所有属性的时候,用 leaks 检测,发现这里出现了内存泄漏。后来查了一些资料发现 class_copyIvarList 返回的对象需要手动释放。因为这些是C实现的函数,需要手动对函数返回值进行 free。具体看代码:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ Class viewControllerClass = NSClassFromString((NSString *)sender); if ([segue.destinationViewController isKindOfClass:viewControllerClass]) { if (_param) { id objec = segue.destinationViewController; unsigned int count = 0; Ivar *ivarList = class_copyIvarList(viewControllerClass, &count); for (int i = 0; i < count; i ++) { Ivar ivar = ivarList[i]; NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)]; ivarName = [ivarName substringFromIndex:1]; id value = _param[ivarName]; if (value) { [objec setValue:value forKey:ivarName]; } } free(ivarList); } } }另外,class_copyMethodList 方法返回的值也需要手动释放。
相关文章推荐
- iOS 使用class_copyPropertyList等runtime方法的注意事项
- class_copyIvarList方法获取实例变量问题引发的思考
- class_copyPropertyList与class_copyIvarList区别,获取属性,使用runtime方法的注意事项
- IOS开发笔记之十四——使用系统相册或相机导致状态栏隐藏的问题(bug总结四)
- iOS开发 Block的使用方法和规范,以及可能遇到的问题
- iOS --- 使用runtime解决3D Touch导致UIImagePicker崩溃的问题
- ios开发使用cocoapods倒入一堆的三方库之后开始崩溃了。发觉是导入极光引用的iOS10UserNotifications.framework导致的问题 Reason: image not found
- iOS --- 使用runtime解决3D Touch导致UIImagePicker崩溃的问题
- Symbian S60开发,在SettingList中使用RadioButton的问题及解决方法。
- Runtime基础使用场景-拦截替换方法(class_addMethod ,class_replaceMethod和method_exchangeImplementations)
- IOS开发:关于UIPickerView类的代理方法titleForRow和viewForRow的使用问题
- 【iOS开发-117】block为什么用copy?利用runtime运行时的objc_方法为分类扩充成员变量
- iOS Runtime详解之SEL,Class,id,IMP,_cmd,isa,method,Ivar
- 运行时简单使用-->获取对象的属性(class_copyIvarList)
- iOS Runtime详解之SEL,Class,id,IMP,_cmd,isa,method,Ivar
- ios开发中使用copy类型的block时,防止循环利用的方法。
- ios开发使用cocoapods倒入一堆的三方库之后开始崩溃了。发觉是导入极光引用的iOS10UserNotifications.framework导致的问题 Reason: image not found
- iOS 开发 深入浅出Runtime运行时之方法动态处理(Dynamic Method Resolution)详解
- Runtime方法的使用—Method、Ivar、Property篇
- class_copyIvarList和class_copyMethodList