我有个问题。
我首先创建了一个扩展 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;
我预计dealloc
要调用每个 Employee 对象的方法,我会看到一些日志,例如:
deallocating.. Employ ID 0 has value.....
deallocating.. Employ ID 2 has value.....
....
但是,我没有看到任何日志,如果我在dealloc
方法中,断点永远不会被命中。
有什么想法吗?
一些观察结果:
person = nil
不会释放非 ARC 代码中的对象。它将在 ARC 代码中(至少如果它很强大的话)。
在 ARC 中,本地对象超出范围时会自动为您释放。在非 ARC 中,超出范围的对象不会为您释放(如果您在其他地方没有对这些对象的其他引用,则最终会导致泄漏)。
将项目添加到可变数组中会增加该项目的保留计数,因此即使您在非 ARC 代码中包含释放,该对象也不会被释放,直到保留计数降至零(通过不仅释放来实现)将 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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)