转载自:http://www.crifan.com/ios_warning_arc_semantic_issue_assigning_retained_object_to_unsafe_property_object_will_be_released_after_assignment_app_error_exc_bad_access/
【问题】BirdWatching的iOS app,现在想要去多多折腾,搞懂不同property的setter修饰符:assign,copy,retain等的更深层的含义。所以,专门去把代码改为:
?
1
2
3//@property (nonatomic, weak) UIImagePickerController *imgPickerController;
//@property (nonatomic) UIImagePickerController *imgPickerController;
@property (nonatomic, assign) UIImagePickerController *imgPickerController;改了之后,结果就是.m文件中初始化的代码:
?
1self.imgPickerController = [[UIImagePickerController alloc] init];出现了警告:ARC Semantic Issue,Assigning retained object to unsafe property;object will be released after assignment,然后程序运行,也出错了:assigning retained object to unsafe property【解决过程】1.根据之前的学习,对于assign等setter的含义为:The Objective-C Programming Language – Declared PropertiesSetter SemanticsThese attributes specify the semantics of a set accessor. They are mutually exclusive.strongSpecifies that there is a strong (owning) relationship to the destination object.weakSpecifies that there is a weak (non-owning) relationship to the destination object.If the destination object is deallocated, the property value is automatically set to nil.(Weak properties are not supported on OS X v10.6 and iOS 4; use assign instead.)copySpecifies that a copy of the object should be used for assignment.The previous value is sent a release message.The copy is made by invoking the copy method. This attribute is valid only for object types, which must implement the NSCopying protocol.assignSpecifies that the setter uses simple assignment. This attribute is the default.You use this attribute for scalar types such as NSInteger and CGRect.retainSpecifies that retain should be invoked on the object upon assignment.The previous value is sent a release message.In OS X v10.6 and later, you can use the __attribute__ keyword to specify that a Core Foundation property should be treated like an Objective-C object for memory management:@property(retain) __attribute__((NSObject)) CFDictionaryRef myDictionary;所以,此处代码改为:
?
1@property (nonatomic, assign) UIImagePickerController *imgPickerController;后,加上之前对于assign的学习,知道了此处对于assign,首先是只适用于非对象类的数据,比如NSInteger,也就是,对于对象引用计数的话,没有任何改变。所以,上述的初始化代码部分中的:[[UIImagePickerController alloc] init];得到了一个UIImagePickerController,然后赋值给了self.imgPickerController但是要知道,此处的self.imgPickerController由于是assign,所以没有对于上述得到的UIImagePickerController引用计数增加,即没有打算再用到UIImagePickerController,所以刚生成的UIImagePickerController,因为没有人再用刀它,就自动释放掉了。所以后续对于self.imgPickerController的操作,都是在操作一个没有分配实体对象的空的指针,所以肯定都是无效操作,肯定就会出现EXC_BAD_ACCESS错误了。就是之前C语言中的野指针的意思了,只是有个指针变量而已,而指针所指向的物理内存,早已被释放掉了,所以你再继续操作此块物理内存,就会出现异常操作了。2.对应的,去改为:
?
1
2
3
4//@property (nonatomic, weak) UIImagePickerController *imgPickerController;
//@property (nonatomic) UIImagePickerController *imgPickerController;
//@property (nonatomic, assign) UIImagePickerController *imgPickerController;
@property (nonatomic, retain) UIImagePickerController *imgPickerController;然后运行结果就是OK的了。3.相应的,也基本明白了,之前对于写成weak的话:
?
1
2
3
4@property (nonatomic, weak) UIImagePickerController *imgPickerController;
//@property (nonatomic) UIImagePickerController *imgPickerController;
//@property (nonatomic, assign) UIImagePickerController *imgPickerController;
//@property (nonatomic, retain) UIImagePickerController *imgPickerController;然后对于此处的和imgPickerController全部相关的代码是:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19- (void)viewDidLoad
{self.imgPickerController = [[UIImagePickerController alloc] init];self.imgPickerController.delegate = self;}  //handle the tap to image
-(void)handleImageTap:(UITapGestureRecognizer *)sender{if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){NSArray *availableMediaTypeArr = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];self.imgPickerController.mediaTypes = availableMediaTypeArr;[self presentViewController:self.imgPickerController animated:YES completion:NULL];}
}其中,运行到handleImageTap的时候,在view画面切换的时候:
?
1[self presentViewController:self.imgPickerController animated:YES completion:NULL];程序会出错。所以,内部的逻辑,应该是:最开始用:
?
1self.imgPickerController = [[UIImagePickerController alloc] init];初始化后,就是一个weak弱引用了,意思是,如果后者,即alloc的UIImagePickerController被dealloc的话,那么前者self.imgPickerController就自动设置为nil了。但是由于当前程序,显示控件中,一直使用到了UIImagePickerController,所以一直也没什么问题。但是当调用presentViewController画面切换的时候,就自动去dealloc释放了,那个引用为0的,之前alloc的UIImagePickerController,所以,导致此时self.imgPickerController也就自动变为nil了。所以程序会出错了。4.对应的,当程序再改为:
?
1
2
3
4//@property (nonatomic, weak) UIImagePickerController *imgPickerController;
@property (nonatomic) UIImagePickerController *imgPickerController;
//@property (nonatomic, assign) UIImagePickerController *imgPickerController;
//@property (nonatomic, retain) UIImagePickerController *imgPickerController;后,此时默认的是strong,即owning的效果了,所以引用计数为1了,所以即使画面切换,由于self.imgPickerController对于UIImagePickerController的引用计数还是1,没有变为0,所以,后续的self.imgPickerController指针指向的,是真正存在的UIImagePickerController对象,所以程序可以正常执行的。即默认的strong和retain的效果是一致的。5.对应的,后来也看到:iOS 5中的strong和weak关键字中说,weak相当于assign,strong相当于retain,但是看着还是很晕。6.最后看到这里:Weak and strong property setter attributes in Objective-C解释的很清楚:对于单个文件,根据设置,可以开启或关闭ARC。如果用了ARC,则不能使用retain,release,autorelease等等修饰符,而只能:针对属性property,使用weak,strong;针对变量variable,使用__weak,__strong;strong等价于retain;weak等价于assign;只有一种情况下需要用到weak:当你想要避免循环引用的时候,才会考虑用weak;因为如果都用strong的话,有可能出现,父类retain子类,而子类retain父类,即循环引用了,导致两者始终都无法释放。此时就可以用weak避免此情况。另外还有个toll free bridging的部分,不多解释,有空看官网解释:Core Foundation Design Concepts – Toll-Free Bridged Types而关于此部分的内容,相关的官网解释,在这里:Transitioning to ARC Release Notes有空需要好好看看。

【已解决】iOS程序出现警告:ARC Semantic Issue,Assigning retained object to unsafe property;object will be releas相关推荐

  1. 【已解决】程序文件被ESET NOD32误杀或拦截怎么办?以ENDPIINT SECURITY为例添加信任教程截图(ESET通用))

    [已解决]程序文件被ESET NOD32误杀或拦截怎么办?以ENDPIINT SECURITY为例添加信任教程截图(ESET通用)) 程序文件可信无病毒,确被"误杀"误伤?如果安装 ...

  2. HTML实现点击时的阴影(:active)(已解决iOS微信上无法使用)

    一般是用这个委类:active 参考:http://www.w3school.com.cn/cssref/selector_active.asp 但是业界上实现的效果中,今日头条的手机端网页却不是这样 ...

  3. [已解决]Hibernate程序未终止

    Recently I was writing a small hibernate program and noticed that the program was not terminating ev ...

  4. 一个利用html5的图片裁剪功能(已解决ios压扁缩放等bug)

    推荐: 这一篇文章是早年为了解决图片裁剪的探索性文章,现在已经开放出了falsh版及html5版本的图片裁剪插件,各位有时间可以看看: 浮士德html5图片裁剪器2016开源版 浮士德头像裁剪flas ...

  5. 【已解决】程序异常终止:Process finished with exit code -1073741819 (0xC0000005)

    一.问题出现背景: 项目环境:Maven + IDEA + JDK1.8 使用Run执行程序时直接终止并出现上述提示信息Process finished with exit code -1073741 ...

  6. 已解决 iOS XIB 拖线奔溃

    奔溃信息 this class is not key value coding-compliant for the key 变量名.' terminating with uncaught except ...

  7. (已解决)iOS真机运行 Xcode报错(libpng error: CgBI: unhandled critical chunk)

    Cocos2d-x加载图片资源出现libpng error: CgBI: unhandled critical chunk  Xcode7.3  设置Remove Text Metadata From ...

  8. 【已解决】Something went wrong. If this issue persists please contact us through our help center at help.

    openai在使用chatgpt对话时频繁出现: Something went wrong. If this issue persists please contact us through our ...

  9. 消除Xcode iOS项目的警告

    消除Xcode iOS项目的警告 作者  犯傻小二  关注 2016.03.10 15:22*  字数 8287  阅读 3839 评论 0 喜欢 7 如果你有强迫症,看到项目中那么多警告肯定特别心烦 ...

  10. 详解优化iOS程序性能的25个方法

    本篇文章主要介绍了优化iOS程序性能的25个方法,感兴趣的小伙伴们可以参考一下 1. 用ARC管理内存 ARC(Automatic ReferenceCounting, 自动引用计数)和iOS5一起发 ...

最新文章

  1. pytorch的backward
  2. MySQL Online DDL的改进与应用
  3. ThinkPHP验证码类
  4. 在Tableau中去除选择高亮效果
  5. CF1286D-LCC【动态dp,数学期望】
  6. Git 切换提交历史节点
  7. Swift 类的使用class
  8. chrome 内核的浏览器pdf 字体显示问题
  9. 【评测】阿法埃莎 (Alfa Aesar)化学品目录
  10. Oracle中TO_NUMBER()函数的用法
  11. 梦回山海推广码jn0010 梦回山海推广码
  12. 【Idea】换行快捷键
  13. 用USART接口代替SPI怎么做?
  14. linux虚拟光驱路径,linux虚拟光驱怎么用?
  15. Module and Component
  16. OpenAI 宣布将对战 DOTA2 世界冠军 OG,最终决战!
  17. realme真我V15国潮锦鲤手机:携《国家宝藏》IP筑开年之作
  18. X-Y非线性关系或U型倒U型曲线的检验
  19. 【领域驱动设计】四色建模法
  20. FFT学习笔记(DFT,IDFT)

热门文章

  1. EditPlus 使用 json 格式化
  2. 快捷键Ctrl+s快速保存,屏蔽保存网页到本地
  3. 蓝桥杯 ALGO-45算法训练 调和数列问题
  4. 一文看懂量子十问(上篇)
  5. C盘扩容(步骤记录)
  6. 微软服务器系统版本有几个,windows系统有几个版本
  7. linux运维工作内容及岗位要求
  8. python unmatched_Python
  9. Spring Boot入门教程(三十八):支付宝集成-电脑网站支付和查询对账单下载地址
  10. 理解标准差、标准化、协方差、正态分布