Miscellanies of iOS 3) Managing Memory with ARC

pp101 

Not just copy and paste anymore...              For the usage of review

1. All Objective-C Objects are stored in a part of memory called the heap. When we send an alloc message to a class, a chunk of memory is allocated from the heap. This  chunk includes space for the object's instance variables. 

2. There is another part of memory called the stack that is separate from the heap. The reason for the names heap and stack has to do with how we visualize them. The heap is a giant heaping mess of objects, and we use pointers to remember where those objects are stored within the heap. The stack, on the other hand, can be visualized as a physical stack of frames. 

When an application launches and runs the main function, the frame for main is put at the bottom of the stack. When main calls another method or function....

3. Pointer Variables and Object Ownership

! Pointer variables convey ownership of the objects that they point to.

a) When a method or function has a local variable that points to an object, that method is said to own the object being pointed to.

b) When an object has an instance variable that points to another object, the object with the pointer is said to own the object being pointed to.

4. The idea of object ownership help us determine whether an object should be destroyed.

a) An object with no owners should be destroyed. An ownerless object cannot be sent messages and isolated and useless to application. Keeping it around wastes precious memory. This is called a memory leak.

b) An object with at least one owner must not be destroyed. Sending messages to an object that no longer exists will crash your application. This is premature deallocation.

5. Automatic Reference Counting in iOS 5 or later version

    Manual Reference Counting before

dont need to keep track of who owns whom and what pointers still exist.

6. How objects lose owners       pp109

a) A variable that points to the object is changed to point to another object.

b) A variable that points to the object is set to nil.       (The object used to be pointed to loses an ownership)

c) A Variable that points to the object is itself destroyed. ?

When a pointer variable itself is destroyed, the object that the variable was pointing at loses an owner. At what point a pointer variable will get destroyed depends on whether it is a local variable or an instance variable.

Recall that instance variables live in the heap as part of an object. When an object gets destroyed, its instance variables are also destroyed, and any object that was pointed to by one of those instance variables loses an owner. 

Local variables live in the method's frame. When a method finishes executing and its frame is popped off the stack, any object that was pointed to by one of these local variables loses an owner.

7. NSObject implements -(void)dealloc method

by setting items = nil;                (dealloc)

At the end, there are no more objects taking up memory, and only the main function remains. All this automatic clean-up and memory recycling occurs simply by setting items to nil. That's the power of ARC.

8. Strong and Weak References      

Under ARC, an iVar will be strong reference to an object by default.

For object types, the default has changed. The compiler is smart enough to know that, if I have a property that is an object, it should be strong, not assign.

For non-ARC, the assign is the default, which is the same as __unsafe_unretain in ARC

So far, we've said that anytime a pointer variable stores the address of an object, that object has an owner and will stay alive. This is known as a strong reference. However, a variable can optionally not take ownership of an object it points to. A variable that does not take ownership of an object is known as a weak reference. 

A weak reference is useful for an unusual situation called a retain cycle. A retain cycle occurs when two or more objects have strong references to each other.  They will never be destroyed by ARC.  cause a memory leak      pp112

p114

To declare a variable as a weak reference, we use the __weak attribute. 

__weak BNRItem *container;

Every retain cycle can be broken down into a parent-child relationship. A parent typically keeps a strong reference to its child, so if a child needs a pointer to its parent, that pointer must be a weak reference to avoid a retain cycle.          so does the parent's parent

9. An interesting property of weak references is that they know when the object they reference is destroyed. 

10. __unsafe_unretained attribute, like a weak reference, but it does not take ownership of the object it points to. Unlike a weak reference, an unsafe unretained reference is not automatically set to nil when the object it points to is destroyed. 

this is for the backward compatibility: application prior to iOS 5.

11. When you declare a property, you are implicitly declaring a setter and a getter for the instance variable of the same name. ? not _pname?

12 nonatomic property attribute is not the default option, so, explicitly declare it.

13. A readwrite property declares both a setter and getter, and a readonly property just declares a getter. And the readwrite is the default.

14.  The third attribute of a property describes its memory management. The default option is assign, which is for properties like int. pp117

15. synthesize the property and override for our own version of getter and setter if needed.

16. If there is no instance variable that matches the name of a synthesized property, one is automatically created.

17. copy property attributes

@property (nonatomic,copy)NSString *itemName;
-(void)setItemName:(NSString *)str
{
	itemName = [str copy];
}

in terms of ownership, copy gives you a strong reference to the new object pointed to. The original object is not modified in any way: it doesn't gain or lose an owner, and none of its data changes.






















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值