main.m文件:atom
#import <Foundation/Foundation.h> #import "Person.h" #import "Dog.h" int main() { Person *p = [[Person alloc] init]; Dog *d = [[Dog alloc] init]; p.dog = d; d.person = p; return 0; }
#import "Dog.h" @implementation Dog - (void)dealloc { NSLog(@"Dog--dealloc"); } @end
#import "Person.h" @implementation Person - (void)dealloc { NSLog(@"Person--dealloc"); } @endDog.h文件:
#import <Foundation/Foundation.h> @class Person; @interface Dog : NSObject @property (nonatomic,strong) Person *person; @end
#import <Foundation/Foundation.h> @class Dog; @interface Person : NSObject @property (nonatomic, strong) Dog *dog; @end
對於上述main.m文件中紅色部分「人養一條狗,狗有一個主人」,仔根據兩個.h文件中部分,編譯運行時會出現「人strong狗,狗strong人」的狀況,致使p和d佔用的內存沒法被回收,即上圖黃色部分不打印。解決辦法爲:spa
一端用strong,另外一端用weakcode
好比能夠將Dog.h文件中strong改爲weak,就能夠解決循環strong 問題。內存
版權聲明:本文爲博主原創文章,未經博主容許不得轉載。it