年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2106|回复: 0

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

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-1-12 16:32:05 | 显示全部楼层 |阅读模式
    该主题主要内容通过自创一个数组“MyArray”来说明内存管理中的retainCount问题:
    1、创建一个数组_objs[512](C语言方法);
    2、数组中包含三个基本方法 addObject、removeObject、removeAll,通过在对象中添加对象与删除对象来理解内存管理的黄金法则;
    3、创建一个类Dog,做为数组操作的基本元素;
    4、创建一个数组对象array;
    5、通过使用addObject将对象元素dog添加到数组中,dog对象本身完成初始化后会release,但在数据中的元素因为需要使用该对象,所以要保证dog对象本身release时所以权由数组控制;
    6、数组自身被释放(release)后,及时将数组中的对象元素进行释放操作;
    代码实现如下:
    Dog.h
    1. //
    2. //  Dog.h
    3. //  Memory_myArray1
    4. //
    5. //  Created by yusian on 14-1-12.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. @interface Dog : NSObject
    10. {
    11.     int _ID;
    12. }
    13. @property int ID;
    14. @end
    复制代码
    Dog.m
    1. //
    2. //  Dog.m
    3. //  Memory_myArray1
    4. //
    5. //  Created by yusian on 14-1-12.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "Dog.h"
    9. @implementation Dog
    10. @synthesize ID = _ID;
    11. - (void) dealloc{
    12.     NSLog(@"dog %d is dealloc",_ID);
    13.     [super dealloc];
    14. }
    15. @end
    复制代码
    myArray.h
    1. //
    2. //  myArray.h
    3. //  Memory_myArray1
    4. //
    5. //  Created by yusian on 14-1-12.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. #import "Dog.h"
    10. @interface myArray : NSObject
    11. {
    12.     NSUInteger _count;
    13.     id _objs[512];
    14. }
    15. @property (assign, readonly) NSUInteger count;
    16. - (void) addObject:(id)object;
    17. - (void) removeObjectAtIndex:(NSUInteger)index;
    18. - (void) removeAll;
    19. @end
    复制代码
    myArray.m
    1. //
    2. //  myArray.m
    3. //  Memory_myArray1
    4. //
    5. //  Created by yusian on 14-1-12.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "myArray.h"
    9. @implementation myArray
    10. @synthesize count = _count;
    11. - (id) init{
    12.     self = [super init];
    13.     if (self) {
    14.         _count = 0;
    15.     }
    16.     return self;
    17. }
    18. - (void) addObject:(id)object{
    19.     if (_count >= 512)
    20.         return;
    21.         _objs[_count] = [object retain];//此步骤是关键!!!(对象在这里进行retain操作,因为对象自身会做release操作,数组中的指针会因此而变成野指针)
    22.         _count ++;
    23. }
    24. - (void) removeObjectAtIndex:(NSUInteger)index{
    25.     id obj = _objs[index];
    26.     [obj release];
    27.     _objs[index] = nil;
    28. }
    29. - (void) removeAll{
    30.     for (int i = 0; i < _count; i ++){
    31.         [self removeObjectAtIndex:i];
    32.     }
    33. }
    34. - (void) dealloc{
    35.     NSLog(@"myArray is dealloc");
    36.     [self removeAll];
    37.     [super dealloc];
    38. }
    39. @end
    复制代码
    main.m
    1. //
    2. //  main.m
    3. //  Memory_myArray1
    4. //
    5. //  Created by yusian on 14-1-12.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. #import "myArray.h"
    10. #import "Dog.h"
    11. int main(int argc, const char * argv[])
    12. {
    13.     @autoreleasepool {
    14.         myArray * array = [[myArray alloc] init];
    15.         
    16.         for (int i = 0; i < 3; i ++){
    17.             Dog * dog = [[Dog alloc] init];
    18.             [dog setID:i];
    19.             [array addObject:dog];
    20.             [dog release];
    21.             NSLog(@"dog %d retainCount is %ld",[dog ID],[dog retainCount]);
    22.         }
    23.         
    24.         //NSLog(@"%ld",[array count]);
    25.         
    26.         [array release];
    27.         
    28.     }
    29.     return 0;
    30. }
    复制代码
    运行结果:
    1. 2014-01-12 16:31:55.660 Memory_myArray1[2950:303] dog 0 retainCount is 1
    2. 2014-01-12 16:31:55.662 Memory_myArray1[2950:303] dog 1 retainCount is 1
    3. 2014-01-12 16:31:55.662 Memory_myArray1[2950:303] dog 2 retainCount is 1
    4. 2014-01-12 16:31:55.662 Memory_myArray1[2950:303] myArray is dealloc
    5. 2014-01-12 16:31:55.663 Memory_myArray1[2950:303] dog 0 is dealloc
    6. 2014-01-12 16:31:55.663 Memory_myArray1[2950:303] dog 1 is dealloc
    7. 2014-01-12 16:31:55.663 Memory_myArray1[2950:303] dog 2 is dealloc
    8. Program ended with exit code: 0
    复制代码

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-5 18:37 , Processed in 0.043663 second(s), 18 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表