内存管理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类对象所拥有 ...
随机推荐
- 怎样给回调函数绑定this
在三种绑定this的方法中, Function.prototype.call() 和 Function.prototye.apply() 都是会立即执行该函数的, 但回调函数是不能立即执行的, 它只是 ...
- JavaScript检测浏览器
Detect Browser <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- [JZOJ5897]密匙--哈希骚操作
[JZOJ5897]密匙--哈希骚操作 题目链接 太懒了自行Google 前置技能 二分/倍增求LCP e.g TJOI2017DNA 分析 这题看了样例解释才知道什么意思 本以为自己身为mo法师蛤希 ...
- [NOIP2018模拟赛10.22]咕咕报告
闲扯 这是篇咕咕了的博客 考场上码完暴力后不知道干什么,然后忽然发现这个T1好像有点像一道雅礼集训时讲过的CF题目 Rest In Shades ,当时那道题还想了挺久不过思路比较妙,于是我就也\(y ...
- Unity NavMesh 格式 解析 分析 对比 Recast Navigation
工具软件 Excel Nodepad++ Sublime Unity 5.4 / 5.6 VS RecastDemo CodeBlocks 分析过程以Unity项目-Demo13为例 一. 创建测试模 ...
- 从graphql endpoint获取schema文件
graphql server端有更新,client端需要重新获取schema文件用于创建新的api request,下面简要记录如何从graphql endpoint获取schema文件 You ca ...
- 数据库之sqlite
数据创建数据 CREATE TABLE IF NOT EXISTS ArpAudit (ID INTEGER PRIMARY KEY autoincrement NOT NULL, UserName ...
- 【大数据】SmallFile-Analysis-Script
1.root账号先在namenode节点上配置一个定时任务,将fsimage定时传到其他客户机上进行操作 whereis hadoop命令确定安装目录,然后去配置文件找到namenode节点(data ...
- Java实现 Cookie的生成与读取
今天学习的时候发现Cookie挺有意思的,就自己网上找点例子,自己敲点代码熟练下,现在就记录下来,分享一下. 什么是cookie?? Cookie 是一小段文本信息,伴随着用户请求和页面在 Web 服 ...
- tesseract图像识别验证码:安装使用和避免坑
安装使用 https://blog.csdn.net/kk185800961/article/details/78747595 避免的坑 http://www.mamicode.com/info-de ...