有没有办法“找到神秘的保留”...?(Is there a way to “find mystery retains” …?)

编程入门 行业动态 更新时间:2024-10-25 12:16:37
没有办法“找到神秘的保留”...?(Is there a way to “find mystery retains” …?)

最近我正在修复某人的代码。 有一个不会释放的大班级。 你必须用5或6个版本才能达到dealloc。

我仔细审视了大班,最终找到了需要发布的各种东西。

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

那么,是否有一种简单的方法可以在对象上“查找所有保留” ? XCode或乐器中是否有其他人都知道的按钮?

当你找不到神秘的东西时,你会怎么做呢?

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

PS请注意,没有泄漏,这个问题与泄漏完全无关。 简单地“完全正确”的对象不会释放。


稍后..

Fabio真正惊人的解决方案:

法比奥为这个问题提供了一个惊人的解决方案。 用九个词来表示:

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

这在许多情况下非常有用,并导致许多其他有用的事情。 法比奥,你可能永远每年为我节省了两个人工周。 谢谢!

顺便说一句,如果你刚刚掌握了这一点,并努力与输出,我看到通常会有很多“UINib instantiateWithOwner:”特色的块。 看起来这些将会首先出现,重要的块将会跟随。

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?

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?

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

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


Later ..

Truly astounding solution by 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!

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.

最满意答案

只是猜测......但你可以覆盖自定义类的超级方法,并抛出一个很好的NSLog来打印调用堆栈。


用Joe的实际代码更新

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

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

注意:在ARC环境中,您需要首先将-fno-objc-arc到编译器标志以允许您覆盖保留并调用super。

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.

更多推荐

本文发布于:2023-07-29 20:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319430.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   神秘   find   mystery   retains

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!