将对象设置为 nil 时不调用 dealloc 方法 [英] dealloc method is not invoked when set an object to nil

查看:136
本文介绍了将对象设置为 nil 时不调用 dealloc 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.

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

I first created an object which extends NSObject, I provided overrides for the description and dealloc methods. Here's my Employee.m file:

@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 对象列表:

In my main.m, I first created an NSMutableArray to hold a list of Employee objects:

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 方法,我会看到一些日志,如:

I expected the dealloc method of each Employee object to be called and I would see some logs like:

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

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

However, I didn't see any logs and if I set a breakpoint on the dealloc method, the breakpoint is never hit.

有什么想法吗?

推荐答案

几点意见:

  1. person = nil 不会在非 ARC 代码中释放对象.它将在 ARC 代码中(至少如果它很强大).

  1. person = nil does not release an object in non-ARC code. It will in ARC code (at least if it's strong).

在 ARC 中,本地对象超出范围时会自动为您释放.在非 ARC 中,不会为您释放超出范围的对象(如果您在别处没有对这些对象的其他引用,最终会导致泄漏).

In ARC, local objects will be released for you automatically when they fall out of scope. In non-ARC, objects falling out of scope will not be released for you (and if you don't have other references to those objects elsewhere, you'll end up with a leak).

将项目添加到可变数组会增加项目的保留计数,因此即使您在非 ARC 代码中包含发布,该对象也不会在保留计数降至零之前被释放(通过不仅在将 person 对象添加到数组后释放它们,而且还从数组中删除它们来实现.

Adding an item to a mutable array will increase the retain count of the item, so even if you include a release in your non-ARC code, the object won't be released until the retain count drops to zero (accomplished by not only releasing the person objects after you add them to the array, but also removing them from the array.

因此,鉴于这是非 ARC 代码,它可能类似于:

Thus, given that this is non-ARC code, it could be something like:

- (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
}

这篇关于将对象设置为 nil 时不调用 dealloc 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆