Instrumentsの Leaksテンプレートを使って循環参照を図示する
Xcode付属の Instruments で大変見やすく図で循環参照を表示してくれる.
やり方は以下の通り
Xcodeでビルドする > Xcodeメニューの Product > Profile > Leaks template > 実行し赤い棒が出たらそこを選択して 左のメニューのLeaks を選択 > Cycles & Roots を選択
ソースコードを編集したらビルドし直して Profileを再起動しなければならなかった.
テスト用ソース
// AppDelegate.m #import "AppDelegate.h" @interface MyClass : NSObject @property (nonatomic) MyClass*next; @property (nonatomic, weak) MyClass*weakNext; @end @implementation MyClass - (void)dealloc{ NSLog(@"MyClass obj is dealloced!"); } @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { MyClass*a=[MyClass new]; MyClass*b=[MyClass new]; MyClass*c=[MyClass new]; a.next = b; b.next = c; c.next = a; // 循環参照発生 // c.weakNext = a; return YES; } @end