Sian 发表于 2014-1-12 16:32:05

第十九讲:Objective-C内存管理之“MyArray”

该主题主要内容通过自创一个数组“MyArray”来说明内存管理中的retainCount问题:
1、创建一个数组_objs(C语言方法);
2、数组中包含三个基本方法 addObject、removeObject、removeAll,通过在对象中添加对象与删除对象来理解内存管理的黄金法则;
3、创建一个类Dog,做为数组操作的基本元素;
4、创建一个数组对象array;
5、通过使用addObject将对象元素dog添加到数组中,dog对象本身完成初始化后会release,但在数据中的元素因为需要使用该对象,所以要保证dog对象本身release时所以权由数组控制;
6、数组自身被释放(release)后,及时将数组中的对象元素进行释放操作;
代码实现如下:
Dog.h
//
//Dog.h
//Memory_myArray1
//
//Created by yusian on 14-1-12.
//Copyright (c) 2014年 yusian. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Dog : NSObject
{
    int _ID;
}

@property int ID;

@end
Dog.m
//
//Dog.m
//Memory_myArray1
//
//Created by yusian on 14-1-12.
//Copyright (c) 2014年 yusian. All rights reserved.
//

#import "Dog.h"

@implementation Dog

@synthesize ID = _ID;

- (void) dealloc{
    NSLog(@"dog %d is dealloc",_ID);
    ;
}
@end
myArray.h
//
//myArray.h
//Memory_myArray1
//
//Created by yusian on 14-1-12.
//Copyright (c) 2014年 yusian. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Dog.h"

@interface myArray : NSObject
{
    NSUInteger _count;
    id _objs;
}

@property (assign, readonly) NSUInteger count;

- (void) addObject:(id)object;
- (void) removeObjectAtIndex:(NSUInteger)index;
- (void) removeAll;

@end
myArray.m
//
//myArray.m
//Memory_myArray1
//
//Created by yusian on 14-1-12.
//Copyright (c) 2014年 yusian. All rights reserved.
//

#import "myArray.h"

@implementation myArray

@synthesize count = _count;

- (id) init{
    self = ;
    if (self) {
      _count = 0;
    }
    return self;
}

- (void) addObject:(id)object{
    if (_count >= 512)
      return;
      _objs = ;//此步骤是关键!!!(对象在这里进行retain操作,因为对象自身会做release操作,数组中的指针会因此而变成野指针)
      _count ++;
}

- (void) removeObjectAtIndex:(NSUInteger)index{
    id obj = _objs;
    ;
    _objs = nil;
}

- (void) removeAll{
    for (int i = 0; i < _count; i ++){
      ;
    }
}

- (void) dealloc{
    NSLog(@"myArray is dealloc");
    ;
    ;
}

@end
main.m
//
//main.m
//Memory_myArray1
//
//Created by yusian on 14-1-12.
//Copyright (c) 2014年 yusian. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "myArray.h"
#import "Dog.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {

      myArray * array = [ init];
      
      for (int i = 0; i < 3; i ++){
            Dog * dog = [ init];
            ;
            ;
            ;
            NSLog(@"dog %d retainCount is %ld",,);

      }
      
      //NSLog(@"%ld",);
      
      ;
      
    }
    return 0;
}
运行结果:
2014-01-12 16:31:55.660 Memory_myArray1 dog 0 retainCount is 1
2014-01-12 16:31:55.662 Memory_myArray1 dog 1 retainCount is 1
2014-01-12 16:31:55.662 Memory_myArray1 dog 2 retainCount is 1
2014-01-12 16:31:55.662 Memory_myArray1 myArray is dealloc
2014-01-12 16:31:55.663 Memory_myArray1 dog 0 is dealloc
2014-01-12 16:31:55.663 Memory_myArray1 dog 1 is dealloc
2014-01-12 16:31:55.663 Memory_myArray1 dog 2 is dealloc
Program ended with exit code: 0

页: [1]
查看完整版本: 第十九讲:Objective-C内存管理之“MyArray”