2025-06-20

最近這個項目在用ARC,在追蹤內存的時候,發現內存不對。
這樣的情況下就不會調用dealloc方法:
@interface Obj1 : NSObject 

    Obj2 *obj2;    

 
@interface Obj2 : NSObject 


 
@implementation Obj1 
 
-(void)dealloc 

    //obj2 = nil;  // <— This is needed to get obj2 to be dealloc'd. 
    NSLog(@"Obj1 dealloc"); 

 
-(id)init 

    if ((self = [super init]) == nil) 
        return nil; 
 
    obj2 = [[Obj2 alloc] init]; 
 
    return self; 

 
@end 
 
@implementation Obj2 
 
-(void)dealloc 

    NSLog(@"Obj2 dealloc"); 

 
-(id)init 

    if ((self = [super init]) == nil) 
        return nil; 
 
    return self; 

 
@end 

如果obj1的dealloc斷點斷下來瞭,那麼也會調用obj2的dealloc也會斷下來,可是無論如何也不會斷。 開始我在obj1的dealloc加obj2 = nil,讓其回收內存,可是ARC有這個功能,不需要加這行呀。  一直無解呀,代碼沒問題,最後check 設置(弄瞭好多個小時),原來開啟瞭NSZombieEnabled。汗水都出來瞭,上次吃瞭NSZombieEnabled的虧,這次又栽在它手上。不過總算找到原因瞭,不過這幾個小時都做的無用功,隻有長點經驗值。

 

摘自 開心程序

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *