内存管理2-set方法的内存管理-程序解析
创建class Book
.h 有@ property float price; //@synthesize 自动
------------
创建class Student
#import "Book.h"
.h 有@property int age;
@property Book *book; //@synthesize 自动
--------------
验证Student对象是否回收
Student.m
-(void)dealloc{
NSLog(@"Student :%i 被销毁了",_age);
[super dealloc];
}
--------------
为了方便访问_age 创建可以传入_age 的构造方法
Student.h
-(id)initWithAge:(int)age;
student.m
-(id)initWithAge:(int)age{
if(self=[super init]){ //如果super返回的对象不为空
_age=age;
}
return self;
}
----------------------
验证Book对象是否回收
Book.m
-(void)dealloc{
NSLog(@" book:%f 被销毁了",_price);
[super dealloc];
}
----------------------------
//with _price Book constructor
Book.h
-(void)initWithPrice:(float)price;
Book.m
-(void)initWithPrice:(float)price{
if(self=[super init]){ //如果super返回的对象不为空
_price=price;
}
}
----------------------------
annotation method
1 #pragma mark constructor
(advantage:easy:locate)
2 #pragma mark -groupname
(advantage :group)
3 Of course // can work
----------------------------
main.m
#import "Book.h"
#import "Student.h"
main(){
@autoreleasepool{
Student *stu=[[Student alloc]initWithAge:10]; //stu 1
Book *book =[[Book alloc]initWithPrice:3.5];// book 1
stu.book=book;// In reality never use this way//book 1
[book release];//book 0
[stu release];//stu 0
}
return 0;
}
---------------------------------
Student.m
//manually realize getter & setter
//Standard realization of getter and setter
Student.h // Because u do manually so XCode will not call @synthesize, so there is no //_book,so we need to state _book ourself.
@interface Student:NSObject{
Book *_book;
}
Student.m
-(void)setBook:(Book *)book{
_book=book;
}
-(Book *)book{
return _book;
}
// Abopve six line can be short for
//@synthesize book=_book;
---------------------------------------
Then counter
-----------------------------------------
// When we developing in reality ,we may use object
//so change code
//main.m
void test (Student *stu){
Book *book=[[Book alloc]initWithPrice:3.5];
stu.book=book;
[book release];
}
void test1(Student *stu){
//add reading method
//Student.h -(void)readBook;
//Student.m
//-(void)readBook{
//NSLog(@"Reading book is:%f",_book.price);
//}
[stu readBook];// not safe ,visiting _book,because in test book is released so (wild pointer)
}
-------------------------
//change code
main(){
test(stu);
test1(stu);
}
-----------------------------
//student want to make use of book object so, retain book in Student
//setBook retain;stu alloc release
---------------------------------
//Improve code
// so compare the passed book object with current book object if not the same one,
//release the previous one.
-(void)setBook:(Book *)book{
if(self.book!=book){
[_book release]; // empty pointer is safe.
_book=[book retain];
}
}
//被释放(-1)和被销毁(0)不同
内存管理2-set方法的内存管理-程序解析的更多相关文章
- 七.OC基础加强--1.内存管理 2.野指针,内存泄露 3.set方法的内存管理 4.@property参数 5.@class和循环retain的使用 6.NSString的内存管理
1,内存管理简单介绍 1,为什么要有内存管理? malloc selloc dealloc```需要回头复习 一般的内存 4s 是512m内存:6 是1024m内存: 当内存过大时,会耗尽内存.出现程 ...
- 分享.net常见的内存泄露及解决方法
分享.net常见的内存泄露及解决方法 关于内存泄漏的问题,之前也为大家介绍过,比如:<C++中内存泄漏的检测方法介绍>,是关于C++内存泄漏的.今天为大家介绍的是关于.NET内存泄漏的问题 ...
- 关于内存管理/set/get方法
MRC状态下 1 任何继承NSObject的对象,存放于堆控件中,都需要手动管理内存 .2 基本数据类型放到栈中,对象放到堆空间中,内存是有系统管理的.(int\float\enum\struct) ...
- OC中的属性、方法及内存管理
普通方法:关注(代表)对象可以”干什么”,过程中需要实例变量.-(void)show;输出 … 访问属性 属性:属性专门处理实例变量.(程序执行过程当中) 初始化方法:一创建对象(第一时间 ...
- OC:内存管理、dealloc方法、copy知识点
属性的声明:使⽤@property声明属性 例如:@property NSString *name: 相当于@interface中声明了两个⽅法(setter.getter): 属性的实现:使⽤@s ...
- set方法的内存管理细节
一.多个对象之间的内存管理 1.你想使用(占用)某个对象,就应该让对象的计数器+1(让对象做一次retain操作) 2.你不想再使用(占用)某个对象,就应该让对象的计数器-1(让对象做一次releas ...
- Android内存管理(5)*官方教程:Logcat内存日志各字段含义,查看当前内存快照,跟踪记录内存分配,用adb查看内存情况时各行列的含义,捕获内存快照的3种方法,如何让程序暴漏内存泄漏的方法
Investigating Your RAM Usage In this document Interpreting Log Messages 内存分析日志中各消息的含 ...
- 【转载】Unity 优雅地管理资源,减少占用内存,优化游戏
转自:星辰的<Unity3D占用内存太大的解决方法> 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大. 这里写下关于Unity3D对于内存的管理与优化. Unity3D ...
- DPDK内存管理-----(二)rte_mempool内存管理
DPDK以两种方式对外提供内存管理方法,一个是rte_mempool,主要用于网卡数据包的收发:一个是rte_malloc,主要为应用程序提供内存使用接口.本文讨论rte_mempool.rte_me ...
- iOS 非ARC基本内存管理系列 2-多对象内存管理(3) 利用@property来自动管理内存
iOS 基本内存管理-多对象内存管理(2)中可以看到涉及到对象的引用都要手动管理内存:每个对象都需要写如下代码 // 1.对要传入的"新车"对象car和目前Person类对象所拥有 ...
随机推荐
- PHP运行机制和原理
以echo "Hello World";为例 经历五个步骤:1.扫描(scanning):先进行语法分析和词法分析,然后将index.php内容变成一个个语言片段(token ar ...
- 在论坛中出现的比较难的sql问题:9(触发器专题 插入数据自动更新表数据)
原文:在论坛中出现的比较难的sql问题:9(触发器专题 插入数据自动更新表数据) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所 ...
- 在论坛中出现的比较难的sql问题:8(递归问题 树形结构分组)
原文:在论坛中出现的比较难的sql问题:8(递归问题 树形结构分组) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有必 ...
- 货币转换B
描述 人民币和美元是世界上通用的两种货币之一,写一个程序进行货币间币值转换,其中: ...
- js数组破坏性和非破坏性方法
数组原型方法:破坏性.会改变数组. shift().unshift().pop().push().splice();resver(),sort().在对数字排序的时候不能用原来的方法了,那样会导致值溢 ...
- MYSQL的操作命令
一.御前 1 win+R DOS 输入 net start mtsql 和 net stop mysql 启动和停止Mysql 服务,也可通过计算机——管理——服务和应用程序——服务——MYSQL— ...
- MySQL全同步复制基于GR集群架构实现(Centos7)
目录 一. 理论概述 概述 二. 部署 向组加入新节点 测试 三.总结 一. 理论概述 概述 本案例操作的是针对于MySQL的复制类型中的全同步复制,对几种复制类型简单总结下: 异步复制:MySQL默 ...
- Image Processing and Analysis_15_Image Registration: A Method for Registration of 3-D shapes——1992
此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...
- 两个linux服务器之间免密登录
服务器A(假设为10.64.104.11) 免密登录服务器B(10.64.104.22) 1.登录服务器A 2.生成公私钥 ssh-keygen -t rsa 3.将生成的.pub文件发送到服务器B上 ...
- 网页报警提示 This page includes a password or credit card input in a non-secure context. A warning has been added to the URL bar. For more information, see https://goo.gl/zmWq3m.
This page includes a password or credit card input in a non-secure context. A warning has been added ...