内存管理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多进程编程
先看下我已经安装的php版本 PHP (cli) (built: Jul ::) ( NTS ) Copyright (c) - The PHP Group Zend Engine v3.- Zend ...
- oracle学习笔记:update一整列 关联更新
普通的 update 都是根据条件来对部分列的内容进行修改,用法如下: update temp_cwh_table set name = 'xxx' where id = 1; 假设现在有2张表:A. ...
- Lua table直接索引VS缓存索引性能测试小示例
local p = {} p.t = {} p.t.p = {} p.t.p.t = {} p.t.p.t.p = {} p.t.p.t.p.t = {} p.t.p.t.p.t.p = {} p.t ...
- Java 面向对象(三)static 关键字
一.static 1.概述 static 的意思的静态的,也是一种修饰符. 关于 static 关键字的使用,它可以用来修饰的成员变量和成员方法,被修饰的成员是属于类的,而不是单单属于某个对象的. 用 ...
- Xmind ZEN破解版来袭:如何去除水印
Xmind ZEN是一款十分优雅地思维导图软件,但是找不到其破解版,在导出图片时就会携带上水印. image-20190110110013642.png 当然,土豪请(点击这里关闭). image-2 ...
- SAP官方提供的人脸识别API
https://api.sap.com/api/face_detection_api/resource 准备一张克里斯蒂亚诺 - 罗纳尔多的图片: 点击Choose File按钮,加载这些图片,然后点 ...
- MySQL sql_mode 说明(及处理一起 sql_mode 引发的问题)
1. MySQL莫名变成了 Strict SQL Mode 最近测试组那边反应数据库部分写入失败,app层提示是插入成功,但表里面里面没有产生数据,而两个写入操作的另外一个表有数据.因为 insert ...
- border边框设置为1px
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- IIS 自动化发布工具实现-Manager【二】
思路: 1.首先是要获取项目的差异文件列表,实现方式是通过cmd 执行git 命令. git pull 拉取最新代码 git log 查看git签入记录 ,使用参数 --pretty=for ...
- Java字节码整体分析与总结
上一次[https://www.cnblogs.com/webor2006/p/9508341.html]已经将编译器生成的默认构造方法的字节相关的分析完了,接下来则分析咱们自定义的方法啦,按照顺序来 ...