OC 内存管理(retain和release)
内存管理 retain和release简单使用
#import "Student.h" @implementation Student
@synthesize age = _age; // 在xcode4.5环境下可以省略 - (void)dealloc {
NSLog(@"%@被销毁了", self); [super dealloc];
// 一定要调用super的dealloc方法,而且最好放在最后面调用
}
@end
#import <Foundation/Foundation.h>
#import "Student.h" void test() {
Student *stu = [[Student alloc] init]; // 1 // z代表无符号
NSLog(@"count:%zi", [stu retainCount]); [stu retain]; // NSLog(@"count:%zi", [stu retainCount]); [stu release]; // NSLog(@"count:%zi", [stu retainCount]); [stu release]; // 0 // [stu release]; // 会发生野指针错误,也就是说访问了不属于你的内存
} void test1() {
// Student对象的计数器永远为1,所以不可能被释放
[[Student alloc] init].age = ; [Student new].age = ; // 上面的代码都有内存泄露
} int main(int argc, const char * argv[])
{
@autoreleasepool { }
return ;
}
对象之间的内存管理
#import "Student.h" @implementation Student #pragma mark - 生命周期方法
#pragma mark 构造方法
- (id)initWithAge:(int)age {
if ( self = [super init] ) {
_age = age;
} return self;
} #pragma mark 回收对象
- (void)dealloc {
// 释放Book对象
[_book release]; // [self.book release]; NSLog(@"student:%i 被销毁了", _age);
[super dealloc];
} #pragma mark - getter和setter
// @synthesize book = _book;
// 如果自己手动实现了getter和setter,xcode就不会自动生成@synthesize
// 也就不会自动生成_book
// getter和setter的默认实现
- (void)setBook:(Book *)book {
if (_book != book) {
// 先释放旧的成员变量
[_book release];
// 再retain新传进来的对象
_book = [book retain];
}
} - (Book *)book {
return _book;
} #pragma mark - 公共方法
#pragma mark 读书
- (void)readBook {
NSLog(@"当前读的书是:%f", _book.price);
} //#pragma mark - 私有方法
//#pragma mark 私有方法1
//- (void)test1 {
//
//
//}
//#pragma mark 私有方法2
//- (void)test2 {
//
//
//}
//#pragma mark 私有方法3
//- (void)test3 {
//
//
//} @end
@property的参数
#import <Foundation/Foundation.h> @class Book;
@class Card; @interface Student : NSObject // 这里的retain代表:在set方法中,release旧值,retain新值
@property (nonatomic, retain) Book *book; @property (retain) Card *card; // readonly代表只生成get方法的声明
// 默认是readwrite,同时生成get和set方法的声明
@property (readonly) int age; // atomic就代表给方法进行加锁,保证线程安全
@property (atomic) int no; // nonatomic代表方法不需要考虑线程安全问题
@property (nonatomic, assign) int no2; // getter是用来指定get方法的方法名
@property (nonatomic, getter = isRich) BOOL rich;
@end
autorelease
#import <Foundation/Foundation.h>
#import "Student.h" int main(int argc, const char * argv[])
{
// @autoreleasepool代表创建一个自动释放池
@autoreleasepool {
Student *stu = [[[Student alloc] init] autorelease]; //[stu autorelease]; Student *stu1 = [[[Student alloc] init] autorelease];
//[stu1 autorelease]; // 这个stu2是自动释放的,不需要释放
Student *stu2 = [Student student]; // 这个str是自动释放的,不需要释放
NSString *str = [NSString stringWithFormat:@"age is %i", ]; for (int i = ; i<; i++) {
[Student student];
}
}
return ;
}
OC 内存管理(retain和release)的更多相关文章
- Objective-C 内存管理retain和release
OC使用引用计数来管理内存,每个继承NSObject的对象,内部都维护了一个引用计数器retainCount.当对象创建时(调用alloc或者new)引用计数器会+1, 手动调用retain()方法能 ...
- OC 内存管理之手动内存管理MRC
一.基本原理 1.什么是内存管理 内存管理的重要性: 移动设备的内存极其有限,每个app所能占用的内存是有限制的 当app所占用的内存较多时,系统会发出内存警告,这时得回收一些不需要再使用的内存空间. ...
- OC 内存管理机制总结
OC 内存管理机制总结 一:OC内存管理机制目前分为两块,其一自动内存管理机制,其二手动内存管理机制: 1.首先我们从自动内存管理机制讲起: 1)什么是自动内存管理机制,自动内存管理机制就是程序中所创 ...
- OC内存管理基础
OC 内存管理基础 一. retain和release基本使用 使用注意: 1.你想使用(占用)某个对象,就应该让对象的计数器+1(让对象做一次retain操作) 2.你不想再使用(占用)某个对象,就 ...
- QF——OC内存管理详解
堆的内存管理: 我们所说的内存管理,其实就是堆的内存管理.因为栈的内存会自动回收,堆的内存需要我们手动回收. 栈中一般存储的是基本数据类型变量和指向对象的指针(对象的引用),而真实的对象存储在堆中.因 ...
- OC内存管理-OC笔记
内存管理细节:http://blog.sina.com.cn/s/blog_814ecfa90102vus2.html 学习目标 1.[理解]内存管理 2.[掌握]第一个MRC程序 3.[掌握]内存管 ...
- OC内存管理-黄金法则
1.内存管理-黄金法则 The basic rule to apply is everything that increases the reference counter with alloc, [ ...
- OC内存管理总结,清晰明了!
<span style="font-size:18px;">OC内存管理 一.基本原理 (一)为什么要进行内存管理. 由于移动设备的内存极其有限.所以每一个APP所占的 ...
- 31 (OC)* 内存管理
31 (OC) 内存管理 一:内存管理黄金法则. 如果对一个对象使用了alloc.[Mutable]copy,retain,那么你必须使用相应的realease或者autorelease 二:内存管 ...
随机推荐
- linux命令之find
find find命令的格式:find [-path……] -options [-print -exec -ok] path:要查找的目录路径. ~ 表示$HOME目录 . 表 ...
- Ansible 命令相关模块command, shell, raw, expect, script, telnet[转]
本文主要介绍Ansible的几个命令模块,包括: command - 在远程节点上执行命令 shell - 让远程主机在shell进程下执行命令 script - 将本地script传送到远程主机之后 ...
- 记自己的hexo个人博客
https://othercoding.github.io/
- 吴恩达《Machine Learning Yearning》总结(11-20章)
11.何时修改开发集.测试集和度量指标 开展一个新项目,尽快选好开发集和测试集:例子,根据度量指标A分类器排在B分类器前面,但是团队认为B分类器在实际产品上优于A分类器,这时就需要考虑修改开发集和测试 ...
- fabric省略输出
fab -f test_fabric.py start --hide status,running,stdout,user,aborts,warnings,stderr 省略所有输出--hide st ...
- 循环结构 while
while 循环语句可以根据某些条件重复执行一条t-sql 语句或一个语句块 语法: while (条件) begin 语句或语句块 end 程序调试 alt+f5 启动调试 f9 切换断点 f10 ...
- 微信token验证源码分享(c#版)
在开发时遇到一个问题: 上线后提交申请微信提示"您的服务器没有正确响应token验证...",我查看日志发现根本就没有接收到来自微信的参数. 后来我又记录了微信请求方式和请求的字符 ...
- mvc 中Request[""]与Request.QueryString[""]
1.Request[""]与Request.QueryString[""]获取不到值时返回null: 2.Request[""]与Reque ...
- setInterval()设置页面5,4,3,2,1秒后跳转
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【JavaWeb】JSP九大内置对象
内置对象特点: 1. 由JSP规范提供,不用编写者实例化. 2. 通过Web容器实现和管理 3. 所有JSP页面均可使用 4. ...