Is there a way to “find mystery retains” …?

后端 未结 6 1116
离开以前
离开以前 2020-12-22 19:22

Recently I was repairing someone\'s code. There was a big class that would not dealloc. You\'d have to hit it with 5 or 6 releases to get it to dealloc.

I carefully

6条回答
  •  忘掉有多难
    2020-12-22 19:46

    Just guessing... but you may overwrite the retain method of the custom class calling super and throwing a nice NSLog to print the call stack.


    Update with the actual code from Joe

    -(id) retain {
    NSLog(@"%@", [NSThread callStackSymbols]);
    return ([super retain]);
    }
    

    Another important detail is that [NSThread callStackSymbols] returns a NSArray of NSStrings that can be filtered and used for other purposes. For example in complex and dynamic code, to check if a method properly causes another one to fire.

    NOTE: In an ARC environment you will need to first add the -fno-objc-arc to compiler flags to allow you to override retain and call super.

提交回复
热议问题