﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-张运涛-文章分类-iphone 测试</title><link>http://www.cppblog.com/zhangyuntaoshe/category/16175.html</link><description>c++</description><language>zh-cn</language><lastBuildDate>Fri, 04 Mar 2011 12:08:05 GMT</lastBuildDate><pubDate>Fri, 04 Mar 2011 12:08:05 GMT</pubDate><ttl>60</ttl><item><title>【转】 iPhone Instruments工具使用</title><link>http://www.cppblog.com/zhangyuntaoshe/articles/141099.html</link><dc:creator>张运涛</dc:creator><author>张运涛</author><pubDate>Fri, 04 Mar 2011 04:00:00 GMT</pubDate><guid>http://www.cppblog.com/zhangyuntaoshe/articles/141099.html</guid><wfw:comment>http://www.cppblog.com/zhangyuntaoshe/comments/141099.html</wfw:comment><comments>http://www.cppblog.com/zhangyuntaoshe/articles/141099.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhangyuntaoshe/comments/commentRss/141099.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhangyuntaoshe/services/trackbacks/141099.html</trackback:ping><description><![CDATA[
<div class="shareUser" style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: 20px; color: rgb(102, 102, 102); text-align: left; ">转载自&nbsp;<a href="http://hi.baidu.com/pangxl%5F1981" target="blank" style="color: rgb(46, 167, 224); text-decoration: none; ">PangXL_1981</a></div><div class="shareLastEditor" style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: 20px; color: rgb(102, 102, 102); text-align: left; margin-top: 5px; margin-right: 0px; margin-bottom: 8px; margin-left: 0px; ">最终编辑&nbsp;<a href="http://hi.baidu.com/superdev" target="blank" style="color: rgb(46, 167, 224); text-decoration: none; ">superdev</a></div><table style="table-layout: fixed; color: rgb(76, 76, 76); font-family: Arial; font-size: 12px; line-height: 18px; width: 874px; "><tbody><tr><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: 18px; "><div id="blog_text" class="cnt" style="font-family: Arial; word-wrap: break-word; word-break: normal; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 14px !important; line-height: 22px; overflow-x: hidden; overflow-y: hidden; position: static; "><p style="line-height: normal; font-size: 14px !important; ">最近常使用Instruments这个工具，我发现它对追踪游戏中的内存泄露非常有帮助。自从发现Instruments如此有用后，我就觉得写一篇文章介绍如何使用它来追踪内存泄露对其他人也会有帮助。<br style="line-height: normal; "><br style="line-height: normal; ">什么是内存泄露？我为什么要关心内存泄露？<br style="line-height: normal; "><br style="line-height: normal; ">&#8230;此段省略&#8230;<br style="line-height: normal; ">访问维基百科可以获得更多关于内存泄露的信息。<br style="line-height: normal; "><br style="line-height: normal; ">我如何知道内存泄露了？<br style="line-height: normal; "><br style="line-height: normal; ">一些内存泄露可以很容易地通过阅读<span style="line-height: normal; font-size: 14px !important; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(250, 137, 27); cursor: pointer; ">代码</span>来发现，另一些就要困难点了，这就是为什么需要Instruments 的原因。Instruments 有一个&#8220;Leaks&#8221;工具，它会准确地告诉你什么地方发生了内存泄露，以便你能定位和修复泄露问题。<br style="line-height: normal; "><br style="line-height: normal; ">例子程序<br style="line-height: normal; "><br style="line-height: normal; ">我写了一个例子程序，它有两个地方会发生内存泄露，一个在 Objective-C&nbsp;<span style="line-height: normal; font-size: 14px !important; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(250, 137, 27); cursor: pointer; ">视图</span>控制器中，另一个在 C++ 类中。例程可以从这里获得。下边的代码是从例程里摘录的，包含了我们需要追踪内存泄露的代码。<br style="line-height: normal; "><br style="line-height: normal; ">// Leaky excerpts &#8211; see GitHub for complete source<br style="line-height: normal; "><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo">- (<span style="font: 11.0px Menlo; color: #b719a2">void</span>)viewDidLoad {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #430481"><span style="color: #000000"><span class="Apple-tab-span" style="white-space:pre">	</span>[super </span>viewDidLoad<span style="color: #000000">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px"><span class="Apple-tab-span" style="white-space:pre">	</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo"><span class="Apple-tab-span" style="white-space:pre">	</span>LeakyClass* myLeakyInstance = new LeakyClass();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo"><span class="Apple-tab-span" style="white-space:pre">	</span>delete myLeakyInstance;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px"><span class="Apple-tab-span" style="white-space:pre">	</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo"><span class="Apple-tab-span" style="white-space:pre">	</span>mMyLeakyString = [[<span style="color: #732fa9">NSString</span> <span style="color: #430481">alloc</span>] <span style="color: #430481">initWithUTF8String</span>:&#8221;I&#8217;m a leaky string.&#8221;];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px"><span class="Apple-tab-span" style="white-space:pre">	</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo"><span class="Apple-tab-span" style="white-space:pre">	</span>[self doSomethingNow];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo">- (<span style="font: 11.0px Menlo; color: #b719a2">void</span>) doSomethingNow</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo">{</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo"><span class="Apple-tab-span" style="white-space:pre">	</span>mMyLeakyString = [[<span style="color: #732fa9">NSString</span> <span style="color: #430481">alloc</span>] <span style="color: #430481">initWithUTF8String</span>:</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo"><span class="Apple-tab-span" style="white-space:pre">					</span>&nbsp; &#8220;Look, another alloc, but no release <span style="font: 11.0px Menlo; color: #b719a2">for</span> first <span style="color: #558087">one</span>!&#8221;];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #048706">// Leaky excerpts &#8211; see GitHub for complete source</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo">LeakyClass::LeakyClass()</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo">{</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo"><span class="Apple-tab-span" style="white-space:pre">	</span>mLeakedObject = new LeakedObject();</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px"><br></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo">LeakyClass::~LeakyClass()</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo">{</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo">}</p>我会先在 Debug 模式编译InstrumentsTest，并在&nbsp;<span style="line-height: normal; font-size: 14px !important; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(250, 137, 27); cursor: pointer; ">iPhone</span>&nbsp;上运行。完成这步，我会启动 Instruments。</p><p style="line-height: normal; font-size: 14px !important; "><img src="http://hiphotos.baidu.com/superdev/pic/item/ae0c4ab6ab4f549f37d3ca55.jpg" height="373" width="530" style="line-height: normal; "></p>当你启动 Instruments，你可以从一堆 Instruments 工具里选择你需要的。在左手边选择 iPhone，在右手边的图标里双击&#8220;Leaks&#8221;工具：<br style="line-height: normal; "><p style="line-height: normal; font-size: 14px !important; "><img src="http://hiphotos.baidu.com/superdev/pic/item/bce8b8efbfb6227663d09f55.jpg" height="373" width="530" style="line-height: normal; "></p>之后你会看到下边的窗口：<br style="line-height: normal; "><p style="line-height: normal; font-size: 14px !important; "><img src="http://hiphotos.baidu.com/superdev/pic/item/cfe7e600a82ae1bd09fa9355.jpg" height="404" width="477" style="line-height: normal; "></p>请确保 iPhone 已经连接到了你的电脑，在这个窗口的左上角，你会看到一个<span style="line-height: normal; font-size: 14px !important; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(250, 137, 27); cursor: pointer; ">下拉</span>菜 单，写着&#8220;Launch Executable&#8221;。单击它，并确保选中的是你 iPhone（而不是你的电脑）作为活动设备。然后移动到&#8220;Launch Executable&#8221;，你可以看到一个包含了所有已安装 iPhone 程序的列表。找到你希望运用&#8220;Leaks&#8221;工具的程序（本例中是 InstrumentsTest）并单击它。<br style="line-height: normal; "><p style="line-height: normal; font-size: 14px !important; "><img src="http://hiphotos.baidu.com/superdev/pic/item/a5cd403e1b3218bf3c6d9755.jpg" height="278" width="401" style="line-height: normal; "></p>你已经准备好了。单击红色的&#8220;Record&#8221;按钮，它会启动程序并开始记录程序里的每个内存分配操作。它会每10秒自动地检测内存泄露。<br style="line-height: normal; "><p style="line-height: normal; font-size: 14px !important; "><img src="http://hiphotos.baidu.com/superdev/pic/item/905a44f8f35e02476c22eb55.jpg" height="524" width="213" style="line-height: normal; "></p>你 可以改变多少时间自动检测一次，你也可以手动进行检测（检测内存泄露的时候程序会停顿大约3-5秒钟，如果你想边进行测试边进行内存检测的话，这种停顿将 会干扰到你）。我一般是设置成手动控制，在我需要的时候才单击&#8220;Check for leaks&#8221;按钮（例如：在loading新的游戏模式之后检测一下，在退出游戏返回 MM 的时候检测一下）。单击&#8220;Leaks&#8221;，并使用右上角的 View-&gt;Detail 按钮来设置和查看选项值，在这个例子里，我将其设置成 auto。<br style="line-height: normal; "><p style="line-height: normal; font-size: 14px !important; "><img src="http://hiphotos.baidu.com/superdev/pic/item/1fcc52152e870e1b21a4e955.jpg" height="96" width="319" style="line-height: normal; "></p>程序在运行一段时间之后，自动内存检测将会发现两处内存泄露。太棒了！现在该干什么呢？<br style="line-height: normal; "><p style="line-height: normal; font-size: 14px !important; "><img src="http://hiphotos.baidu.com/superdev/pic/item/a4c08151d187335f0df3e355.jpg" height="413" width="530" style="line-height: normal; "></p>Extended Detail 视图<br style="line-height: normal; "><br style="line-height: normal; ">Instruments 非常懒，它不会明显地指出下一步该干什么。你需要注意的是窗口底部的那一排按钮。看见两个矩形组成的那个按钮了吗？讲你的鼠标停留在上边，它会提示&#8220;Extended Detail View&#8221;。<br style="line-height: normal; "><p style="line-height: normal; font-size: 14px !important; "><img src="http://hiphotos.baidu.com/superdev/pic/item/3515630d4756969b7acbe155.jpg" height="383" width="530" style="line-height: normal; "></p>单击这个按钮，右边将会弹出一个窗口，里边提供了各种关于内存泄露的详细信息。单击一个内存泄露，Extended Detail 视图将会显示泄露的内存代码的完整调用堆栈。在我们上边的例子中，单击第一个内存泄露提示，它发生在 [NSString initWithUTF8String]。如果你选中调用堆栈里的高亮步骤，你会看到程序最后一次调用是 [InstrumentsTestViewController viewDidLoad]。<br style="line-height: normal; "><br style="line-height: normal; ">双击 Extend Detail 视图中的某行，它会<span style="line-height: normal; font-size: 14px !important; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(250, 137, 27); cursor: pointer; ">打开</span><span style="line-height: normal; font-size: 14px !important; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(250, 137, 27); cursor: pointer; ">&nbsp;XCode</span>&nbsp;窗口并显示出问题的代码，这是非常棒的功能。<br style="line-height: normal; "><p style="line-height: normal; font-size: 14px !important; "><img src="http://hiphotos.baidu.com/superdev/pic/item/d6fc1b45bdbaff586a63e555.jpg" height="398" width="530" style="line-height: normal; "></p>在本例中，第一次 NSString 分配的时候出现了泄露，你需要做一些处理。这是个非常简单的例子，但找到为什么会发生泄露则要麻烦些。让我们仔细看一下例子。在 viewDidLoad 当中，我们为字符串分配到了内存，如下所示：<br style="line-height: normal; "><br style="line-height: normal; ">mMyLeakyString = [[NSString alloc] initWithUTF8String:&#8221;I&#8217;m a leaky string.&#8221;];<br style="line-height: normal; "><br style="line-height: normal; ">在 dealloc 当中我们用如下方式来<span style="line-height: normal; font-size: 14px !important; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(250, 137, 27); cursor: pointer; ">释放</span><br style="line-height: normal; "><br style="line-height: normal; ">[mMyLeakyString release];<br style="line-height: normal; "><br style="line-height: normal; ">你的直觉可能是这样不会发生泄露，但搜索代码中所有用到了 mMyLeakyString 的地方，在 doSomethingNow 中，它是这样用的：<br style="line-height: normal; "><br style="line-height: normal; ">mMyLeakyString = [[NSString alloc] initWithUTF8String:<br style="line-height: normal; ">&#8220;Look, another alloc, but no release for first one!&#8221;];<br style="line-height: normal; "><br style="line-height: normal; ">注意，我们声明了一个新的字符串，并且将 mMyLeakyString 指向了它。这里的问题是我们没有在更改 mMyLeakyString 的指向前释放它原 来指向的内存。所以原始的字符串依然在堆中，并且我们没有办法释放这部分内存。dealloc 里的 release 操作实际释放的是我们在 doSomethingNow 中声明的字符串所占内存，因为这才是指针所指。<br style="line-height: normal; ">为了修复这个问题，我们可以把 doSomethingNow 改成下边的代码：<br style="line-height: normal; "><br style="line-height: normal; ">- (void) doSomethingNow<br style="line-height: normal; ">{<br style="line-height: normal; ">[mMyLeakyString release];<br style="line-height: normal; ">mMyLeakyString = [[NSString alloc] initWithUTF8String:<br style="line-height: normal; ">&#8220;Look, another alloc, but released first one!&#8221;];<br style="line-height: normal; ">}<br style="line-height: normal; "><br style="line-height: normal; ">这段代码做的是在我们指定 mMyLeakyString 到新的字符串前释放第一个字符串所占内存。重新编译运行程序，你会看到只有一个内存泄露。当然，在项目中可能有更好的方式来处理 NSString，但如果你这样处理的话可以修复这个泄露问题。<br style="line-height: normal; ">让我们看看第二个泄露问题。单击泄露提示看什么导致了内存泄露。发现这个泄露来自于 LeakyClass::LeakyClass() 构造函数：<br style="line-height: normal; "><p style="line-height: normal; font-size: 14px !important; "><img src="http://hiphotos.baidu.com/superdev/pic/item/5f28af0a3ac6b7756a60fb55.jpg" height="398" width="530" style="line-height: normal; "></p>在调用堆栈中双击它，出问题的代码将会再次出现在 XCode 中。<br style="line-height: normal; "><p style="line-height: normal; font-size: 14px !important; "><img src="http://hiphotos.baidu.com/superdev/pic/item/c654e72738c54d628644f955.jpg" height="291" width="462" style="line-height: normal; "></p>我们看到在构造函数里声明了一个新的 LeakedObject 对象，但是析构函数没有删除，这样不好。对于每一个 new 操作，都需要有与之对应的 delete 操作。所以我们把析构函数改变成下边的样子：<br style="line-height: normal; "><br style="line-height: normal; ">LeakyClass::~LeakyClass()<br style="line-height: normal; ">{<br style="line-height: normal; ">if (mLeakedObject != NULL)<br style="line-height: normal; ">{<br style="line-height: normal; ">delete mLeakedObject;<br style="line-height: normal; ">mLeakedObject = NULL;<br style="line-height: normal; ">}<br style="line-height: normal; ">}<br style="line-height: normal; "><br style="line-height: normal; ">重新编译运行，没有内存泄露了！<br style="line-height: normal; ">我选择这两个例子，虽然非常简单，但他们展示了 Instruments 可以用来追踪 Object-C 和 C++ 中的内存泄露。<br style="line-height: normal; ">修复你的内存泄露问题吧，记住，没有内存泄露的程序才是一个好程序。<br style="line-height: normal; ">原文出处<a target="_blank" href="http://www.mobileorchard.com/find-iphone-memory-leaks-a-leaks-tool-tutorial/" style="color: rgb(46, 167, 224); text-decoration: none; line-height: normal; ">http://www.mobileorchard.com/find-iphone-memory-leaks-a-leaks-tool-tutorial/</a></div></td></tr></tbody></table><img src ="http://www.cppblog.com/zhangyuntaoshe/aggbug/141099.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhangyuntaoshe/" target="_blank">张运涛</a> 2011-03-04 12:00 <a href="http://www.cppblog.com/zhangyuntaoshe/articles/141099.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>