2012-01-24 38 views
5

可能重複:
Why is object not dealloc'ed when using ARC + NSZombieEnabled詭異的弧線問題不釋放伊娃在UIView子類

我有一個很奇怪的問題,我看到在一個項目的那一刻。簡單地說,我有ViewA,它擁有ViewBstrong屬性)。 ViewA在其初始化程序中創建了其ViewB。這兩個對象都是UIView的子類。

我已經在兩者中重寫了dealloc,並且放置了一個日誌行和一箇中斷點以查看它們是否被擊中。看起來ViewAdealloc被擊中,但不是ViewB的。但是如果我在中輸入deallocViewA那麼就是命中。

所以基本上它是這樣的:

@interface ViewA : UIView 
@property (nonatomic, strong) ViewB *viewB; 
@end 

@implementation ViewA 

- (id)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
     self.viewB = [[ViewB alloc] initWithFrame:self.bounds]; 
     [self addSubview:self.viewB]; 
    } 
    return self; 
} 

- (void)dealloc { 
    //self.viewB = nil; ///< Toggling this commented/uncommented changes if ViewB's dealloc gets called. 
    NSLog(@"ViewA dealloc"); 
} 

@end 

我不能理解的是,爲什麼零-ING viewB出有差別。如果其他東西保持在viewB那麼它應該沒有什麼區別,如果我把它刪除或不在這裏。而且它也不會影響ARC添加的版本數量。

我似乎無法在最小的測試案例中重現它,但我正在研究它。不幸的是,我不能發佈我看到的實際代碼。我不認爲這是一個問題,因爲更重要的一點是,消除它應該不會產生影響,讓我感到困惑。

任何人都可以看到我忽略的任何東西,或給出關於在哪裏尋找調試這個問題的建議?

更新:

我發現這個問題。當NSZombieEnabled設置爲YES時,它似乎只是一個問題。那麼這完全是瘋狂的,肯定是一個錯誤。就我所知,殭屍不應該影響這種工作方式。對象應該仍然通過dealloc方法。而且,如果我在ViewAdealloc中刪除了viewB,它就會生氣。

+0

作爲一個脫離主題,你可以讓它變得「弱」,並忘記這個問題:'[self addSubview:self.viewB]'爲你保留'viewB',所以它不會被過早釋放,即使你保留一個「弱」的引用。 – dasblinkenlight

+0

除非他需要在iOS 4.3上運行。 –

+0

準確地。我希望它可以在iOS 4.x上運行,而且我不應該這樣做**。我不明白爲什麼在'dealloc'中刪除'viewB'會有所作爲。這對我來說似乎很生氣。 – mattjgalloway

回答

4

我發現這似乎是殭屍的iOS實現中的一個錯誤。請看下面的代碼:

#import <Foundation/Foundation.h> 

@interface ClassB : NSObject 
@end 

@implementation ClassB 
- (id)init { 
    if ((self = [super init])) { 
    } 
    return self; 
} 
- (void)dealloc { 
    NSLog(@"ClassB dealloc"); 
} 
@end 

@interface ClassA : NSObject 
@property (nonatomic, strong) ClassB *b; 
@end 

@implementation ClassA 
@synthesize b; 
- (id)init { 
    if ((self = [super init])) { 
     b = [[ClassB alloc] init]; 
    } 
    return self; 
} 
- (void)dealloc { 
    NSLog(@"ClassA dealloc"); 
} 
@end 

int main() { 
    ClassA *a = [[ClassA alloc] init]; 
    return 0; 
} 

應該輸出:

ClassA dealloc 
ClassB dealloc 

但隨着NSZombieEnabled設置爲YES,它輸出:

ClassA dealloc 

據我所知,這是一個錯誤。它似乎只發生在iOS(包括模擬器和設備),並且在Mac OS X上構建和運行時不會發生。我已經向Apple提交了一個雷達。

編輯:原來這已經在這裏回答 - Why is object not dealloc'ed when using ARC + NSZombieEnabled。在我發現真正的問題之後找到了它。順便說一下,這與ARC無關。

+0

offtopic:no [super dealloc];電話? – CarlJ

+1

@meccan - 不,因爲我使用ARC。 – mattjgalloway