加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将对象设置为nil时,不会调用objective-c – dealloc方法

发布时间:2020-12-16 10:31:43 所属栏目:百科 来源:网络整理
导读:我有个问题. 我首先创建了一个扩展NSObject的对象,我提供了描述和dealloc方法的覆盖.这是我的Employee.m文件: @implementation Employee.....-(NSString *)description{ return [NSString stringWithFormat:@"Employ ID: %d has $%d value of assets",[self
我有个问题.

我首先创建了一个扩展NSObject的对象,我提供了描述和dealloc方法的覆盖.这是我的Employee.m文件:

@implementation Employee
.....

-(NSString *)description
{
    return [NSString stringWithFormat:@"Employ ID: %d has $%d value of assets",[self     employeeID],[self valueOfAssets]];
}

-(void)dealloc
{   
    NSLog(@"deallocating.. %@",self);
    [super dealloc];
}

在我的main.m中,我首先创建了一个NSMutableArray来保存Employee对象列表:

NSMutableArray *employees = [[NSMutableArray alloc] init];

for (int i =0; i< 10; i++)
{
    // Create an instance of Employee
    Employee *person = [[Employee alloc] init];

    // Give the instance varaible interesting values
    [person setEmployeeID:i];
    [employees addObject: person];
}

最后我把员工定为零

employees = nil;

我期望调用每个Employee对象的dealloc方法,我会看到一些日志,如:

deallocating.. Employ ID 0 has value.....
deallocating.. Employ ID 2 has value.....
....

但是,我没有看到任何日志,如果我在dealloc方法上设置断点,则断点永远不会被命中.

有什么想法吗?

解决方法

几点意见:

> person = nil不会在非ARC代码中释放对象.它将在ARC代码中(至少如果它很强大).
>在ARC中,当本地对象超出范围时,将自动为您释放它们.在非ARC中,不会为您释放超出范围的对象(如果您在其他地方没有其他对这些对象的引用,则最终会泄漏).
>将项添加到可变数组将增加项的保留计数,因此即使您在非ARC代码中包含一个版本,该对象也不会被释放,直到保留计数降至零(不仅仅由将person对象添加到数组后释放person对象,但也从数组中删除它们.

因此,鉴于这是非ARC代码,它可能是这样的:

- (void)testInNonArcCode
{
    NSMutableArray *employees = [[NSMutableArray alloc] init]; // employees retain count = +1

    for (int i =0; i< 10; i++)
    {
        //create an instance of Employee
        Employee *person = [[Employee alloc] init]; // person retain count = +1

        //Give the instance varaible interesting values
        [person setEmployeeID:i];
        [employees addObject: person];  // person retain count = +2
        [person release];  // person retain count = +1 (YOU REALLY WANT TO DO THIS OR ELSE OR NON-ARC PROGRAM WILL LEAK)
        // person = nil;   // this does nothing,except clears the local var that's limited to the for loop scope ... it does nothing to reduce the retain count or improve memory management in non-ARC code,thus I have commented it out
    }

    // do whatever you want

    [employees removeAllObjects]; // this will remove all of the person objects and they will have their respective retain counts reduced to 0,and therefore the Employee objects will be released

    [employees release]; // employees array's own retain count reduced to zero (and will now be dealloced,itself)
}

在ARC代码中:

- (void)testInArcCode
{
    NSMutableArray *employees = [[NSMutableArray alloc] init]; // employees retain count = +1

    for (int i =0; i< 10; i++)
    {
        //create an instance of Employee
        Employee *person = [[Employee alloc] init]; // person retain count = +1

        //Give the instance varaible interesting values
        [person setEmployeeID:i];
        [employees addObject: person];  // person retain count = +2

        // person = nil;      // this would reduce person retain count to +1 (but unnecessary in ARC because when person falls out of scope,it will have it's retain count automatically reduced)
    }

    // do whatever you want

    [employees removeAllObjects]; // this will remove all of the person objects and they will have their respective retain counts reduced to 0,and therefore will be released

    // [employees release]; // not permitted in ARC
    // employees = nil;     // this would effectively release employees,but again,not needed,because when it falls out of scope,it will be released anyway
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读