[英]What happens exactly, when I set an property to nil in the dealloc method?
[英]dealloc method is not invoked when set an object to nil
我有個問題。
我首先創建了一個擴展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代碼中包含一個版本,該對象也不會被釋放,直到保留計數降至零(不僅僅通過釋放來完成)將對象添加到數組后,也將對象從數組中刪除。
因此,鑒於這是非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
}
解放對象的正確方法就是這樣做
[employees release];
將其設置為nil
將不會釋放內存。
由於您被允許調用[super dealloc]
,我可以假設您沒有使用自動引用計數 。 這意味着您需要顯式地將您編寫的每個alloc
與平衡release
調用配對。 對於你來說,當你使數組為零時,你基本上泄露了員工的所有內存。 您需要再次遍歷數組以釋放它們,或者更好,因為您正在學習... 盡快開始編寫ARC代碼。
值得注意的是,ARC是針對這種情況而創建的; 它對我們的大腦有意義,現在如果你使用最新的工具它可以成為現實。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.