您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: