如何验证ARC模式下的引用计数? [英] How do I verify reference count in ARC mode?

查看:106
本文介绍了如何验证ARC模式下的引用计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经在调试器下使用[myVar retainCount]验证我的一些变量是否具有预期的保留计数,尤其是对于没有自定义dealloc的var。

I used to verify that some of my variables had the expected retain count using [myVar retainCount] under the debugger, especially for var that did not have a custom dealloc.

如何在ARC模式下执行此操作?你如何确保没有内存泄漏?

How do you do this in ARC mode? How do you ensure that there are no memory leaks?

注意:我知道ARC应该为我处理这个问题,但生活远非完美,在现实生活中你有对象有时由第三方库分配(使用retain?)并且永远不会被释放。

Note: I understand that ARC should handle this for me, but life is far from being perfect, and in real life you have objects that are sometimes allocated by third party libraries (using retain?) and never deallocated.

我执行此操作的图像:

MyObj *myObj=[[MyObj alloc] init];

然后我打电话

[somethingElse doSomethingWithMyObj:myObj];

以及稍后,我

myObj=NULL;

如果我的程序工作正常,我的期望是myObj正在被销毁,但似乎没有是这样的......

If my program is working fine, my expectation is that myObj is being destroyed, but it appears not to be the case...

那么如何跟踪这一点,特别是如果我没有管理somethingElse?

So how can I track this, especially if somethingElse is not managed by me?

现在,关于工具:在我的mac(使用5 Meg)上运行内存工具似乎非常困难,无需重新启动mac并从头开始。这真烦人!甚至在程序启动之前仪器就会崩溃,那么是否有更改解决方案?

Now, about the tools: it seems extremely hard to run memory tools on my mac (with 5 Meg) without rebooting the mac and starting from scratch. This is really annoying! Instruments keep crashing even before the program has started, so is there an alterante solution?

推荐答案

您可以使用 CFGetRetainCount 使用Objective-C对象,即使在ARC下:

You can use CFGetRetainCount with Objective-C objects, even under ARC:

NSLog(@"Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)myObject));

这是对调试特别有用,但由于其他地方充分描述的原因。如果您需要了解保留和释放对象的位置,请使用分配工具查看此答案以获取帮助。

This is not particularly useful for debugging, though, for reasons amply described elsewhere. If you need to understand where an object is being retained and released, check out this answer for help using the Allocations instrument.

我发现检查保留计数的唯一情况实际上是有用的是在 dealloc 方法中保留并自动释放要释放的对象。这会在自动释放池耗尽后导致崩溃。您可以通过检查每条消息之前和之后的保留计数来查明原因。通过这种方式,我发现 observationInfo 方法(它本身通常只对调试有用)保留和自动释放 self 。但是,即使是这种问题通常也可以在不检查保留计数的情况下解决,只需将 dealloc 的整个主体包装在 @autoreleasepool block。

The only case I've found where examining the retain count is actually useful is in a dealloc method, when something retains and autoreleases the object being deallocated. This will cause a crash later when the autorelease pool is drained. You can pinpoint the cause of this by checking the retain count before and after each message. In this way I discovered that the observationInfo method (which is itself usually only useful for debugging) retains and autoreleases self. However, even this sort of problem can usually be solved without examining the retain count, simply by wrapping the entire body of dealloc in an @autoreleasepool block.

但是,保留计数可用于了解某些类的实现。 (仅用于娱乐或好奇!不要依赖生产代码中的未记录的实现细节!)

However, the retain count can be used to learn about the implementation of some classes. (Only do this for entertainment or curiosity! Never rely on undocumented implementation details in production code!)

例如,在 @中立即尝试autoreleasepool in main

NSNumber *n0 = [[NSNumber alloc] initWithInt:0];
NSLog(@"0 reference count = %ld", CFGetRetainCount((__bridge CFTypeRef)n0));
// Prints 2 in my test

所以 NSNumber 可能缓存(或至少重用)某些实例。但不是其他人:

So NSNumber likely caches (or at least reuses) some instances. But not others:

n0 = [[NSNumber alloc] initWithInt:200];
NSLog(@"n0 reference count = %ld", CFGetRetainCount((__bridge CFTypeRef) n0));
// Prints 1 - I am the sole owner of this instance.  There could be weak
// or unretained references to it, but no other strong references.

NSNumber *n1 = [[NSNumber alloc] initWithInt:200];
NSLog(@"n1 reference count = %ld", CFGetRetainCount((__bridge CFTypeRef) n1));
// Prints 1 again.  New instance with same value as prior instance.
// You could of course compare pointers to see that they are separate
// instances.

你甚至可以发现 NSNumber 返回一个单身如果你 alloc 但是没有初始化:

You can even discover that NSNumber returns a singleton if you alloc but don't initialize:

n1 = [NSNumber alloc];
NSLog(@"n1 reference count = %ld", CFGetRetainCount((__bridge CFTypeRef) n1));
// Prints -1.

(请注意,您还可以了解有关 NSNumber 查看Core Foundation源代码,该代码可在 http://opensource.apple.com 上找到。但是,如果你看一下Core Foundation中与对象无法免费桥接的对象的保留计数,谁知道你会发现什么?)

(Note that you can also learn many details about NSNumber by looking at the Core Foundation source code, which is available at http://opensource.apple.com. But who knows what you might find if you look at the retain count of objects that aren't toll-free-bridged with objects in Core Foundation?)

这篇关于如何验证ARC模式下的引用计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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