2012-06-24 60 views
3

我有一個問題。將對象設置爲零時,不會調用dealloc方法

我首先創建了一個擴展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方法上設置斷點,斷點永遠不會被命中。

有什麼想法?

+0

問題已經通過將我的項目解決到ARC:編輯>重構>轉換爲Objective-C ARC,以獲取有關ARC的更多信息http://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_4_2.html –

回答

8

幾個觀察:

  1. person = nil不在非ARC代碼釋放物體。它將在ARC代碼中(至少如果它強大的話)。

  2. 在ARC中,本地對象將在超出範圍時自動爲您釋放。在非ARC中,超出範圍的對象將不會被釋放(如果您沒有其他地方的其他對象引用,則最終會發生泄漏)。

  3. 將項目添加到可變數組將增加項目的保留計數,因此即使在非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 
} 
+2

這應該是被接受的答案。我不認爲「改爲ARC」正確回答了這個問題,因爲這個問題意味着這個人出於某種原因不使用ARC(這可能是故意的),並且這個回答是解釋性的,並且也解釋了什麼是ARC做不同的事。 – Accatyyc

3

釋放對象的正確方法是做

[employees release]; 

將其設置爲nil不會釋放內存。

2

由於您允許撥打[super dealloc],我可以假定您沒有使用Automatic Reference Counting。這意味着您需要明確將您編寫的每個alloc與平衡release呼叫進行配對。對於你來說,當你將數組設置爲零時,你基本上泄露了員工的所有內存。您需要再次遍歷數組以釋放它們,或者更好,因爲您正在學習... 儘快開始編寫ARC代碼。

可能很重要的一點是,ARC是爲這種情況而創建的;這對我們的大腦是有意義的,現在如果你使用最新的工具,它可能會成爲現實。

+1

感謝ctrahey ,我已經轉換了我的項目到ARC。它現在可以工作了,我不需要再調用[super dealloc] –

相關問題