有没有办法“找到神秘的保留” ...? [英] Is there a way to "find mystery retains" ...?

查看:108
本文介绍了有没有办法“找到神秘的保留” ...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我正在修复某人的代码。有一个大班不会dealloc。您必须使用5或6个版本来获取它才能获得dealloc。

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 looked through the big class and eventually found the various things that needed to be released.

这让我想到:只需要一些非常简单的方法来找到对象的所有保留 ..我是对的吗?

This got me thinking: there just has to be some really easy way to "find" all the retains on an object .. am I right?

那么,是否有一种简单的方法可以找到对象的所有保留?是否有其他人都知道的XCode或Instruments中的按钮?

So, is there a simple way to "find all the retains" on an object? Is there a button in XCode or Instruments that everyone else knows about?

当你找不到这样的神秘保留时你会怎么做?

What do you do when you can't find a mystery retain like that?

所以在iOS世界中,如果有人知道显示所有保留来自此对象的位置按钮 - 谢谢!

So in the iOS universe, if anyone knows the "Show where all the retains came from on this object" button -- thanks!

PS请注意,没有泄漏,这个问题与泄漏完全无关。对象只是完全正确不会释放。

P.S. Note that there is no leak, and this question is totally unrelated to leaks. The object simply "perfectly correctly" wouldn't release.

稍后..

Fabio提供这个问题令人震惊的解决方案。九个字,这里是:

Fabio has provided an astounding solution to this problem. In nine words, here it is:

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

这在许多情况下非常有用,并且会带来许多其他有用的东西。法比奥,你可能每年都为我节省了两个人一周的工作时间。谢谢!

That is amazingly useful in many situations and leads to many other useful things. You've probably saved me two man-weeks of work per annum forever, Fabio. Thanks!

顺便说一下,如果你刚刚掌握了这个并且在输出上挣扎,我看到通常会有很多以UINib instantiateWithOwner:为特色的块。看起来这些将首先出现,重要的块将会出现。

BTW if you're just getting to grips with this and struggling with the output, I saw that typically there will be many chunks featuring "UINib instantiateWithOwner:". It looks like those will come first, the significant chunks will follow.

推荐答案

只是猜测...但你可能会覆盖保留自定义类的方法调用super并抛出一个好的NSLog来打印调用堆栈。

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.

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

另一个重要细节是[NSThread callStackSymbols]返回NSStrings的NSArray,它可以被过滤并用于其他目的。例如,在复杂和动态代码中,检查方法是否正确导致另一个方法触发。

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.

注意:
在ARC环境中,您需要先将 -fno-objc-arc 添加到编译器标志,以允许覆盖retain和call super。

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.

这篇关于有没有办法“找到神秘的保留” ...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆