xcode 仪器分配跟踪用户定义类的对象的分配和释放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14890402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:46:38  来源:igfitidea点击:

Instruments Allocations track alloc and dealloc of objects of user defined classes

iosobjective-cxcodeinstruments

提问by Cosmin

Is it possible to track the allocation and deallocation of my Objective-C objects? For instance if I have a class Book, I want to track all the allocations and deallocations of the objects of Book type. I can track all the default SKD classes, such as UIViewController, UIWindow, UIView, NSDictionary and all that, but I want to track also the objects that are created from my defined classes.

是否可以跟踪我的 Objective-C 对象的分配和解除分配?例如,如果我有一个 Book 类,我想跟踪 Book 类型对象的所有分配和释放。我可以跟踪所有默认的 SKD 类,例如 UIViewController、UIWindow、UIView、NSDictionary 等等,但我还想跟踪从我定义的类创建的对象。

回答by rob mayoff

You can use the Allocations instrument to track the lifecycle of your objects. If you use the “Allocations” template, it is configured to record mallocand freeevents. You may want to configure it to also record retain, release, and autoreleaseevents by turning on the “Record reference counts” checkbox in the Allocations instrument settings:

您可以使用分配工具来跟踪对象的生命周期。如果您使用“分配”模板,它被配置为记录mallocfree事件。您可能希望通过打开 Allocations 工具设置中的“Record reference counts”复选框将其配置为还记录retainreleaseautorelease事件:

record reference counts checkbox

记录引用计数复选框

(You cannot toggle this while Instruments is recording, which it starts by default as soon as you choose your template.)

(你不能在 Instruments 录音时切换它,默认情况下它会在你选择模板后立即启动。)

Update for Xcode 9:Apple has made it more difficult to find these settings. Now you need to select the Allocations instrument, then from the menu bar choose File > Recording Options.

Xcode 9 更新:Apple 使查找这些设置变得更加困难。现在您需要选择 Allocations 工具,然后从菜单栏中选择 File > Recording Options。

After your run, you can find your objects using the Allocations > Statistics > Object Summary view, which is the default setting for the Detail pane (the bottom half of the window):

运行后,您可以使用 Allocations > Statistics > Object Summary 视图找到您的对象,这是 Detail 窗格(窗口的下半部分)的默认设置:

Object Summary setting for Detail pane

详细信息窗格的对象摘要设置

If you want to see objects that had been deallocated before you stopped the run, you need to change the Allocation Lifespan setting from “Created & Still Living” (the default) to “All Objects Created”:

如果您想查看在停止运行之前已释放的对象,您需要将分配生命周期设置从“创建并仍然存在”(默认)更改为“创建的所有对象”:

Allocation Lifespan setting

分配寿命设置

To find objects of a specific class, start by typing the class name into the Search field at the right end of the window toolbar. Then find the class name in the Category column of the list view, mouse over it, and click the arrow that appears next to it. For example, my app has a class named Tile, so I search for that and then click the arrow next to Tilein the list view:

要查找特定类的对象,首先在窗口工具栏右端的搜索字段中键入类名。然后在列表视图的 Category 列中找到类名,将鼠标悬停在它上面,然后单击它旁边出现的箭头。例如,我的应用程序有一个名为 的类Tile,所以我搜索它,然后单击Tile列表视图中旁边的箭头:

Searching

搜索

Now the list view shows every instance of Tile. (Note that you have to enter the actual class of the object, not a superclass. Entering NSObjectwill only find objects that were created by [NSObject alloc], not objects that were created by [Tile alloc].) I can see the history for any particular instance by clicking the arrow next to that instance's address:

现在列表视图显示Tile. (请注意,您必须输入对象的实际类,而不是超类。输入NSObject只会找到由 创建的[NSObject alloc]对象,而不是由创建的对象[Tile alloc]。)我可以通过单击下一个箭头来查看任何特定实例的历史记录到该实例的地址:

Getting detail

获取细节

In the detail view for an object, I can see the mallocand freeevents and, since I turned on “Record reference counts”, I can also see the retain, release, and autoreleasemessages and their effect on the object's retain count. If I want to see the call stack for any of those events, I can open the extended detail panel on the right side of the window:

在一个对象的详细视图,我可以看到mallocfree事件,并且由于我打开“记录的引用计数”,我还可以看到retainreleaseautorelease对对象的消息及其影响保留计数。如果我想查看任何这些事件的调用堆栈,我可以打开窗口右侧的扩展详细信息面板:

extended detail of call stack

调用堆栈的扩展细节