Don’t Use Accessor Methods in Initializer Methods and dealloc 【初始化和dealloc方法中不要调用属性的存取方法,而要直接调用 _实例变量】
1.问题:
用Xcode的Analyze分析我的Project,会列出一堆如下的提示:
Incorrect decrement of the reference count of an object that is not owned at this point by the caller
仔细看了下代码,都是在dealloc方法中使用了[self.xxx release]这样的语句引起的,把代码改成了[xxx release]就没有问题了
但我不明白究竟原因为何?我的理解是:不管@property xxx这里设定了是retain, copy还是assign,影响的总是setter方法,getter方法不会边,就是简单地return xxx完事了。而self.xxx就是调用[self getXxx]一样的,那么为什么这样的代码会引起Xcode Analyze的警告呢?
2.答案:
self.xxx调用的是getter,而getter并非想当然的是 - (id) xxx{ return _xxx; }.
有可能是 - (id) xxx{ return [[xxx retain] autorelease]; }还有各种情况。这时候你发release就不只是你想的_xxx接收的了。
所以,在dealloc里面可以这样释放:
self.xxx = nil;
或者是 [_xxx release];
这边有两个帖子,我觉得不错:
链接1:stackoverflow.com/questions/7262268/why-shouldnt-i-use-the-getter-to-release-a-property-in-objective-c
链接2:developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-S
3.总结:
Don’t Use Accessor Methods in Initializer Methods and dealloc【初始化和dealloc方法中不要调用属性的存取方法,而要直接调用 _实例变量】。
Accessor methods 存取器方法; 访问方法; 存取方法; 访问函数; 访问器方法;
Don’t Use Accessor Methods in Initializer Methods and dealloc 【初始化和dealloc方法中不要调用属性的存取方法,而要直接调用 _实例变量】的更多相关文章
- 不要在初始化方法和dealloc方法中使用Accessor Methods
苹果在<Advanced Memory Management Programming Guide>指出: Don’t Use Accessor Methods in Initializer ...
- Grid (read-only) objects and methods (client-side reference)获取子表单对象的一些方法 Crm 2016
https://msdn.microsoft.com/en-us/library/dn932126.aspx#BKMK_GridControl Updated: November 29, 2016 A ...
- 怎样理解 Vue 中的计算属性 computed 和 methods ?
需求: 在 Vue 中, 我们可以像下面这样通过在 引号 或 双花括号 内写 js 表达式去做一些简单运算, 这是可以的, 不过这样写是不直观的, 而且在 html 中 夹杂 一些运算逻辑这种做法其实 ...
- Vue中computed(计算属性)、methods、watch的区别
实现效果:字符串的动态拼接 methods方法 html: <div id="app"> <!-- 监听到文本框数据的改变 --> <input ty ...
- Java SE 8 docs——Static Methods、Instance Methods、Abstract Methods、Concrete Methods和field
一.Static Methods.Instance Methods.Abstract Methods.Concrete Methods ——Static Methods:静态方法 ——Instance ...
- http methods & restful api methods
http methods & restful api methods 超文本传输协议(HTTP)是用于传输超媒体文档(例如HTML)的应用层协议 https://developer.moz ...
- vue中computed计算属性与methods对象中的this指针
this 指针问题 methods与computed中的this指针 应该指向的是它们自己,可是为什么this指针却可以访问data对象中的成员呢? 因为new Vue对象实例化后data中的成员和c ...
- Mongoose 'static' methods vs. 'instance' methods
statics are the methods defined on the Model. methods are defined on the document (instance). We may ...
- Vue中methods(方法)、computed(计算属性)、watch(侦听器)的区别
1.computed和methods 共同点:computed能现实的methods也能实现: 不同点:computed是基于它的依赖进行缓存的.computed只有在它的相关依赖发生变化才会重新计算 ...
随机推荐
- ButterKnife 8.2.1 大圣归来
零.前言 ButterKnife是一个视图注入的框架,主要帮我们解决无脑的findViewById.设置监听事件等等体力劳动. 一.引入 好消息是ButterKnife终于使用apt生成代码了,首先在 ...
- Android 应用程序集成Google 登录及二次封装
谷歌登录API: https://developers.google.com/identity/sign-in/android/ 1.注册并且登录google网站 https://accounts. ...
- 学习Maven之Maven Surefire Plugin(JUnit篇)
1.maven-surefire-plugin是个什么鬼? 如果你执行过mvn test或者执行其他maven命令时跑了测试用例,你就已经用过maven-surefire-plugin了.maven- ...
- select、poll、epoll区别总结
1 本质上都是同步I/O 三者都是I/O复用,本质上都属于同步I/O.因为三者只是负责通知应用程序什么时候数据准备好了,实际的I/O操作还是在由应用程序处理:如果是异步I/O的话,实际I/O由内核处理 ...
- 【scikit-learn】scikit-learn的线性回归模型
内容概要 怎样使用pandas读入数据 怎样使用seaborn进行数据的可视化 scikit-learn的线性回归模型和用法 线性回归模型的评估測度 特征选择的方法 作为有监督学习,分类问题是预 ...
- centos7搭建自己的yum源
http://www.wenbin.cf/post/37/ 1. 安装nginx,createrepo yum install nginx -y yum install createrepo -y ...
- Linux系统VNC配置实践总结
VNC概述 VNC (Virtual Network Computing)是虚拟网络计算机的缩写.VNC 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的.VNC 是在 ...
- ORACLE 物理读 逻辑读 一致性读 当前模式读总结浅析
在ORACLE数据库中有物理读(Physical Reads).逻辑读(Logical Reads).一致性读(Consistant Get).当前模式读(DB Block Gets)等诸多概念,如果 ...
- ORACLE调整SGA_TARGET值耗费时间长案例
在一数据库版本为(标准版)Oracle Database 10g Release 10.2.0.4.0 - 64bit Production 的服务器上调整 sga_target时,遇到命令执行了非常 ...
- Redmine 插件安装
将对应的插件都复制进redmine的plugins 安装对应所需要的GEMS bundle install --without development test rmagick 执行插件合并 bund ...