1.问题:   

在dealloc方法中使用[self.xxx release]和[xxx release]的区别?

用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方法中不要调用属性的存取方法,而要直接调用 _实例变量】的更多相关文章

  1. 不要在初始化方法和dealloc方法中使用Accessor Methods

    苹果在<Advanced Memory Management Programming Guide>指出: Don’t Use Accessor Methods in Initializer ...

  2. 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 ...

  3. 怎样理解 Vue 中的计算属性 computed 和 methods ?

    需求: 在 Vue 中, 我们可以像下面这样通过在 引号 或 双花括号 内写 js 表达式去做一些简单运算, 这是可以的, 不过这样写是不直观的, 而且在 html 中 夹杂 一些运算逻辑这种做法其实 ...

  4. Vue中computed(计算属性)、methods、watch的区别

    实现效果:字符串的动态拼接 methods方法 html: <div id="app"> <!-- 监听到文本框数据的改变 --> <input ty ...

  5. Java SE 8 docs——Static Methods、Instance Methods、Abstract Methods、Concrete Methods和field

    一.Static Methods.Instance Methods.Abstract Methods.Concrete Methods ——Static Methods:静态方法 ——Instance ...

  6. http methods & restful api methods

    http methods & restful api methods 超文本传输​​协议(HTTP)是用于传输超媒体文档(例如HTML)的应用层协议 https://developer.moz ...

  7. vue中computed计算属性与methods对象中的this指针

    this 指针问题 methods与computed中的this指针 应该指向的是它们自己,可是为什么this指针却可以访问data对象中的成员呢? 因为new Vue对象实例化后data中的成员和c ...

  8. Mongoose 'static' methods vs. 'instance' methods

    statics are the methods defined on the Model. methods are defined on the document (instance). We may ...

  9. Vue中methods(方法)、computed(计算属性)、watch(侦听器)的区别

    1.computed和methods 共同点:computed能现实的methods也能实现: 不同点:computed是基于它的依赖进行缓存的.computed只有在它的相关依赖发生变化才会重新计算 ...

随机推荐

  1. 在xib中用KVC修改控件属性

    比如我们想在xib 中设置按钮的圆角,这样的话我们就可以在xib文件中进行修改,具体操作如下 实现效果,如下

  2. Reveal1.5破解,iOS_UI调试利器Reveal最新版本破解方法

    Reveal1.0.7破解 1.官网下载最新版Reveal,拖动应用程序中,运行一次2.下载16进制编辑器"0xED" for mac(http://dl.vmall.com/c0 ...

  3. CoreLocation定位技术

    CoreLocation框架可用于定位设备当前经纬度,通过该框架,应用程序可通过附近的蜂窝基站,WIFI信号或者GPS等信息计算用户位置.      iOS定位支持的3种模式.      (1)GPS ...

  4. css sprite 调整大张图片中小图标的大小

    直接说解决方法: 假设一张拼合好的大图大小是:900 x 1000 px (如上图) 现在想取图中左上角的河马图标,并缩小图标的大小. 正常取图: .sprite { background: url( ...

  5. Visual Studio 2015上安装Entity Framework Power Tools

    Entity Framework Power Tools是个非常好用的EF Code First插件.通过它能够非常简单地生成和数据库结构匹配的model和dbcontext代码.使用的方法,这里有介 ...

  6. Red Hat Enterprise Server 6.0 安装Sendmail相关包

    由于需要在Linux服务器(Red Hat Enterprise Linux Server release 6.0)上配置邮件服务,需要安装Sendmail包,一般Sendmail的安装有两种方式:R ...

  7. Linux No volume control GStreamer plugins and/or devices found

    案例环境:Oracle Linux Server release 5.7 进入Oracle Linux系统后,在右上角点击声音图标时,则会弹出如下报错窗口: The volume control di ...

  8. SQL SERVER 2008数据库各版本功能对比

    微软SQL SERVER 2008数据库有6个版本,分别是数据中心版.企业版.标准版.Web版.工作组版.简易版,有时候购买的时候或需要使用某项功能时,需要了解各个版本的区别,功能差异,很多时候,大部 ...

  9. linq to sql 输出SQL语句

    DataClassesDataContext db = new DataClassesDataContext(); db.Log=Response.Output; var result = from ...

  10. 关于nfs共享目录的使用技巧

    nfs客户端的使用 1.查看nfs服务器信息挂载信息 1)在客户端,要查看nfs服务器上有哪些共享目录 # showmount -e nfs服务器ip 在客户端,要查看nfs服务器上有哪些客户端的目录 ...