oc学习之旅:内存管理2修改数组为非ARC版本
2013-12-25 19:10
239 查看
为了重写数组的非arc版本,特意添加了一个Person类,把Person添加到自定义的数组类中,因为是强引用,所以添加完后把Person类释放掉,然后再把每个元素释放掉后会打印Person与自定义数组类的dealloc方法,顺序是先是调用Person的dealloc在调用自定义数组类的dealloc。每个功能都要实现内存管理,这样才不导致内存泄露
#import <Foundation/Foundation.h>
#import "Person.h"
#import "CBMutableArray.h"
int main(int argc,
const char * argv[])
{
@autoreleasepool {
// NSMutableArray *>
CBMutableArray * arr = [[CBMutableArray
alloc]
init];
Person *p = [[Person
alloc] init];
Person *p2 = [[Person
alloc] init];
Person *p3 = [[Person
alloc] init];
Person *p4 = [[Person
alloc] init];
p.str = [NSMutableString
stringWithString:@"wahaha"];
//数组添加对象 retain
[arr
CBAddObject:p];
[arr
CBAddObject:p2];
[arr
CBAddObject:p3];
[arr
CBAddObject:p4];
NSLog(@"p.retainCount is = %ld",p.retainCount);
[p
release];
[p2
release];
[p3
release];
[p4
release];
[arr
CBRemoveAtIndex:1];
Person *p5 = [[Person
alloc] init];
[arr CBReplaceAtIndex:1
withObject:p5];
[p5
release];
[arr
release];
}
return 0;
}
CBMutableArray.h
@interface CBMutableArray :
NSObject
{
id _arr[127];
NSInteger _count;
}
- (void) CBAddObject:(id)obj;
- (void) CBRemoveAllObjects;
- (void) CBRemoveAtIndex:(NSInteger)index;
- (void) CBReplaceAtIndex:(NSInteger)index withObject:(id)obj;
- (id) CBObjectAtIndex:(NSInteger)index;
CBMutableArray.m
- (id)init
{
self = [super
init];
if (self) {
_count = 0;
}
return
self;
}
- (void) CBAddObject:(id)obj
{
if (_arr[_count] !=obj) {
[_arr[_count]
release];
_arr[_count] = [obj
retain];
_count++;
}
}
- (void) CBRemoveAllObjects
{
for (int i =
0; i < _count; i++) {
[_arr[i]
release];
NSLog(@"_arr[%i] release",i);
_arr[i] = nil;
}
_count = 0;
}
- (void) CBRemoveAtIndex:(NSInteger)index
{
if (index > _count-1) {
return;
}
[_arr[index]
release];
// index之后的前移
for (NSInteger i = index; i <=
_count-1; i++) {
_arr[i] = _arr[i+1];
}
_arr[_count] =
nil;
_count--;
}
- (void) CBReplaceAtIndex:(NSInteger)index withObject:(id)obj
{
if (index > _count-1) {
return;
}
if (_arr[index] != obj) {
[_arr[index]
release];
_arr[index] = [obj
retain];
}
}
- (id) CBObjectAtIndex:(NSInteger)index
{
if (index > _count-1) {
return nil;
}
return _arr[index];
}
- (NSString *)description
{
NSMutableString * str = [NSMutableString
stringWithString:@" (\n"];
for (NSInteger i =
0; i < _count; i++) {
//
最后一个元素
if (i == _count-1) {
[str
appendFormat:@" %@\n",_arr[i]];
// 非最后一个元素
}else{
[str
appendFormat:@" %@,\n",_arr[i]];
}
}
[str
appendString:@")"];
return str;
}
-(void)dealloc
{
for (int i =
0; i<_count; i++) {
[_arr[i]
release];
NSLog(@"_arr[%i] dealloc",i);
}
NSLog(@"%@ dealloc",[self
class]);
[super
dealloc];
}
Person.h
@property(nonatomic,assign)
NSString *str;
Person.m
-(void)dealloc
{
NSLog(@"==================\n");
NSLog(@"%@ dealloc",[self
class]);
_str = nil;
[super
dealloc];
}
运行结果
2013-12-25 18:35:45.888 CounstomMSmutableArry[23716:303]>
[b]2013-12-25 18:35:45.890 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.897 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.898 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.899 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.899 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.900 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.900 CounstomMSmutableArry[23716:303] _arr[0] dealloc
2013-12-25 18:35:45.901 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.901 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.901 CounstomMSmutableArry[23716:303] _arr[1] dealloc
2013-12-25 18:35:45.902 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.902 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.903 CounstomMSmutableArry[23716:303] _arr[2] dealloc
2013-12-25 18:35:45.903 CounstomMSmutableArry[23716:303] CBMutableArray dealloc
#import <Foundation/Foundation.h>
#import "Person.h"
#import "CBMutableArray.h"
int main(int argc,
const char * argv[])
{
@autoreleasepool {
// NSMutableArray *>
CBMutableArray * arr = [[CBMutableArray
alloc]
init];
Person *p = [[Person
alloc] init];
Person *p2 = [[Person
alloc] init];
Person *p3 = [[Person
alloc] init];
Person *p4 = [[Person
alloc] init];
p.str = [NSMutableString
stringWithString:@"wahaha"];
//数组添加对象 retain
[arr
CBAddObject:p];
[arr
CBAddObject:p2];
[arr
CBAddObject:p3];
[arr
CBAddObject:p4];
NSLog(@"p.retainCount is = %ld",p.retainCount);
[p
release];
[p2
release];
[p3
release];
[p4
release];
[arr
CBRemoveAtIndex:1];
Person *p5 = [[Person
alloc] init];
[arr CBReplaceAtIndex:1
withObject:p5];
[p5
release];
[arr
release];
}
return 0;
}
CBMutableArray.h
@interface CBMutableArray :
NSObject
{
id _arr[127];
NSInteger _count;
}
- (void) CBAddObject:(id)obj;
- (void) CBRemoveAllObjects;
- (void) CBRemoveAtIndex:(NSInteger)index;
- (void) CBReplaceAtIndex:(NSInteger)index withObject:(id)obj;
- (id) CBObjectAtIndex:(NSInteger)index;
CBMutableArray.m
- (id)init
{
self = [super
init];
if (self) {
_count = 0;
}
return
self;
}
- (void) CBAddObject:(id)obj
{
if (_arr[_count] !=obj) {
[_arr[_count]
release];
_arr[_count] = [obj
retain];
_count++;
}
}
- (void) CBRemoveAllObjects
{
for (int i =
0; i < _count; i++) {
[_arr[i]
release];
NSLog(@"_arr[%i] release",i);
_arr[i] = nil;
}
_count = 0;
}
- (void) CBRemoveAtIndex:(NSInteger)index
{
if (index > _count-1) {
return;
}
[_arr[index]
release];
// index之后的前移
for (NSInteger i = index; i <=
_count-1; i++) {
_arr[i] = _arr[i+1];
}
_arr[_count] =
nil;
_count--;
}
- (void) CBReplaceAtIndex:(NSInteger)index withObject:(id)obj
{
if (index > _count-1) {
return;
}
if (_arr[index] != obj) {
[_arr[index]
release];
_arr[index] = [obj
retain];
}
}
- (id) CBObjectAtIndex:(NSInteger)index
{
if (index > _count-1) {
return nil;
}
return _arr[index];
}
- (NSString *)description
{
NSMutableString * str = [NSMutableString
stringWithString:@" (\n"];
for (NSInteger i =
0; i < _count; i++) {
//
最后一个元素
if (i == _count-1) {
[str
appendFormat:@" %@\n",_arr[i]];
// 非最后一个元素
}else{
[str
appendFormat:@" %@,\n",_arr[i]];
}
}
[str
appendString:@")"];
return str;
}
-(void)dealloc
{
for (int i =
0; i<_count; i++) {
[_arr[i]
release];
NSLog(@"_arr[%i] dealloc",i);
}
NSLog(@"%@ dealloc",[self
class]);
[super
dealloc];
}
Person.h
@property(nonatomic,assign)
NSString *str;
Person.m
-(void)dealloc
{
NSLog(@"==================\n");
NSLog(@"%@ dealloc",[self
class]);
_str = nil;
[super
dealloc];
}
运行结果
2013-12-25 18:35:45.888 CounstomMSmutableArry[23716:303]>
[b]2013-12-25 18:35:45.890 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.897 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.898 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.899 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.899 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.900 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.900 CounstomMSmutableArry[23716:303] _arr[0] dealloc
2013-12-25 18:35:45.901 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.901 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.901 CounstomMSmutableArry[23716:303] _arr[1] dealloc
2013-12-25 18:35:45.902 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.902 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.903 CounstomMSmutableArry[23716:303] _arr[2] dealloc
2013-12-25 18:35:45.903 CounstomMSmutableArry[23716:303] CBMutableArray dealloc
相关文章推荐
- 【OC学习-11】ARC和内存管理里面的alloc、assign、new、retain、copy、mutableCopy、release说明
- -OC学习之内存管理与ARC
- 黑马程序员学习笔记_OC之内存管理3ARC
- -OC学习之内存管理与ARC
- OC ---- 字符串 数组 iOS学习-----细碎知识点总结
- OC-049.集合中的内存管理(MRC和ARC)
- IOS学习---OC基础学习(ARC 协议)
- 三、快速学习创建版本库、管理修改、查看状态、分支、标签、变基、克隆远程版本库
- 【Objective-C学习笔记】内存管理——ARC自动引用计数
- 黑马程序员 OC中的内存管理及MRC和ARC单个对象的内存管理问题
- 黑马程序员——OC学习小结------arc,block,protocol(协议)
- OC学习 第九章 数组字典嵌套
- 黑马程序员--ios基础--oc-内存管理之ARC、Block
- OC学习5——内存管理(上)
- oc学习之旅:内存管理1
- 黑马程序员——OC语言学习——ARC机制、block代码块、protocol协议、protocol协议的应用
- OC学习篇之---数组对象的引用计数问题和自动释放池的概念
- Elasticsearch 5.0版本 学习二、修改数据
- Git学习笔记二(版本回退、工作区和暂存区、撤销修改、删除文件)
- 黑马程序员--OC学习日记-OC内存管理学习感悟