oc学习之旅:内存管理2修改数组为非ARC版本

来源:互联网 发布:淘宝网优依裤 编辑:程序博客网 时间:2024/06/10 06:53

为了重写数组的非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 = [[CBMutableArrayalloc] init];

       Person *p = [[Personalloc] init];

       Person *p2 = [[Personalloc] init];

       Person *p3 = [[Personalloc] init];

       Person *p4 = [[Personalloc] init];


        

        p.str = [NSMutableStringstringWithString:@"wahaha"];

        //数组添加对象 retain

        [arrCBAddObject:p];

        [arrCBAddObject:p2];

        [arrCBAddObject:p3];

        [arrCBAddObject:p4];

        

        NSLog(@"p.retainCount is = %ld",p.retainCount);

        [prelease];

        [p2release];

        [p3release];

        [p4release];

        [arrCBRemoveAtIndex:1];

        

       Person *p5 = [[Personalloc] init];

        [arr CBReplaceAtIndex:1withObject:p5];

        [p5release];

        [arrrelease];

        

        

        


        

    }

   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 = [superinit];

   if (self) {

       _count = 0;

    }

    return self;

}


- (void) CBAddObject:(id)obj

{

   if (_arr[_count] !=obj) {

        [_arr[_count]release];

       _arr[_count] = [objretain];

       _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 = [NSMutableStringstringWithString:@" (\n"];

   for (NSInteger i =0; i < _count; i++) {

        // 最后一个元素

       if (i == _count-1) {

            [strappendFormat:@"    %@\n",_arr[i]];

           // 非最后一个元素

        }else{

            [strappendFormat:@"    %@,\n",_arr[i]];

        }

    }

    [strappendString:@")"];

    

   return str;

}

-(void)dealloc

{

   for (int i =0; i<_count; i++) {

        [_arr[i]release];


        NSLog(@"_arr[%i] dealloc",i);

    }

    NSLog(@"%@ dealloc",[selfclass]);

    [superdealloc];

}


Person.h

@property(nonatomic,assign)NSString *str;


Person.m

-(void)dealloc

{

    NSLog(@"==================\n");

    NSLog(@"%@ dealloc",[selfclass]);

   _str = nil;

    [superdealloc];

}


运行结果

2013-12-25 18:35:45.888 CounstomMSmutableArry[23716:303]>

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



0 0
原创粉丝点击