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. WebView的使用

    1.首先修改activity.xml中的代码: 2.然后MainActivity中的代码: 3.最后设置权限: <uses-permission android:name="andro ...

  2. n个元素的入栈顺序有多少种出栈顺序?

    问题:w1.w2.w3.w4.w5,5个元素将会按顺序入栈,求出栈顺序有多少种情况. 先写一下结论方便记忆: 1个元素:1种 2个元素:2种 3个元素:5种 4个元素:14种 5个元素:42种 简单的 ...

  3. CoreData数据库浅析

    Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象.在此数 ...

  4. 学习Maven之Cobertura Maven Plugin

    cobertura-maven-plugin是个什么鬼? cobertura-maven-plugin是一个校验单元测试用例覆盖率的工具,可以生成一个测试覆盖率报告,可以给单元测试用例编写提供参考. ...

  5. 【转】Linux常用命令大全

    原文地址:http://www.php100.com/html/webkaifa/Linux/2009/1106/3485.html 系统信息 arch 显示机器的处理器架构(1) uname -m ...

  6. 2-2 Linux 根文件系统详解

    根据马哥Linux初级视频2-3 1. 根文件下的一级目录 #ls / 1. / boot 系统启动相关的文件.如内核.initrd   (initialization run directory) ...

  7. 关于android的日志输出&LogCat

    android提供了自己的log输出api-->位于android.util.Log这个类中. 这个类比较常用的打印日志的方法有5个,这5个方法都会把日志打印到LogCat中: Log.v(ta ...

  8. [WPF系列]Adorner应用-自定义控件ImageHotSpot

    引言 项目中有时需要在图片上标注热区,在HTML中有<area>标签,但在WPF中目前还没现成的控件来实现这这一功能.至于图片热区功能有道词典的[图解词典]是个不错的例子,如图1: 图 1 ...

  9. 理解 OpenStack 高可用(HA)(1):OpenStack 高可用和灾备方案 [OpenStack HA and DR]

    本系列会分析OpenStack 的高可用性(HA)概念和解决方案: (1)OpenStack 高可用方案概述 (2)Neutron L3 Agent HA - VRRP (虚拟路由冗余协议) (3)N ...

  10. Windows 10 新特性 -- Bing Maps 3D地图开发入门(一)

    本文主要内容是讲述如何创建基于 Windows Universal App 的Windows 10 3D地图应用,涉及的Windows 10新特性包括 Bing Maps 控件.Compiled da ...