<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>[iPhoneiOS dev:tips]; &#187; Debugging</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/debugging/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:59:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Tracking Down EXC_BAD_ACCESS Errors with NSZombieEnabled</title>
		<link>http://iPhoneDeveloperTips.com/debugging/tracking-down-exc_bad_access-errors-with-nszombieenabled.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/tracking-down-exc_bad_access-errors-with-nszombieenabled.html#comments</comments>
		<pubDate>Thu, 08 Apr 2010 11:47:00 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6091</guid>
		<description><![CDATA[It&#8217;s only a matter of time before you&#8217;ll find yourself face to face with the dreaded EXC_BAD_ACCESS error message. This message generally occurs when you attempt to access an object that has previously been released. NSZombieEnabled is an environment variable which can be helpful to track down the elusive object causing the problem. With the [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s only a matter of time before you&#8217;ll find yourself face to face with the dreaded EXC_BAD_ACCESS error message. This message generally occurs when you attempt to access an object that has previously been released.</p>
<p><strong>NSZombieEnabled</strong> is an environment variable which can be helpful to track down the elusive object causing the problem. With the environment variable set, Cocoa will redirect all objects <strong>isa</strong> pointer to an NZZombie object once an objects retain count has reached zero. In the future, should you try and send a message to a previously released object, an exception will be raised showing a message similar to the following:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/04/zombie1.png" /></p>
<h5>Setting NSZombieEnabled Environment Variable</h5>
<p>In the Groups and Files section, expand the <strong>Executables</strong> section and right click on your app name and choose <strong>Get Info</strong> from the dialog:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/04/zombie3.png" /></p>
<p>Select the <strong>Arguments</strong> tab on the top and then add a new entry in the <strong>Variables to be set in the environment</strong> section. Name the new variable to <strong>NSZombieEnabled</strong> and set its value to <strong>YES</strong>.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/04/zombie2.png" /></p>
<h5>Tracking Down the Error</h5>
<p>Using a zombie, messages sent to an object that has already released will now generate the exception shown above, and following the stack track should now help you find the location of the object that is generating the EXC_BAD_ACCESS error.</p>
<p>Don&#8217;t forget to remove the environment variable, or at a minimum untick the checkbox once you find the source of the problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/tracking-down-exc_bad_access-errors-with-nszombieenabled.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Debugging Macros</title>
		<link>http://iPhoneDeveloperTips.com/debugging/debugging-macros.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/debugging-macros.html#comments</comments>
		<pubDate>Tue, 26 May 2009 12:57:36 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2293</guid>
		<description><![CDATA[In a previous post, Yet Another Debug Output Replacement, I wrote a macro that I prefer over NSLog as the output does not prepend the date/time and object information that NSLog does. Since that time I&#8217;ve pulled together a few more macros that I use on a regular basis for printing debug information on rectangles, [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous post, <a href="http://iphonedevelopertips.com/debugging/yet-another-debug-output-nslog-replacement.html">Yet Another Debug Output Replacement</a>, I wrote a macro that I prefer over NSLog as the output does not prepend the date/time and object information that NSLog does.</p>
<p>Since that time I&#8217;ve pulled together a few more macros that I use on a regular basis for printing debug information on rectangles, points and sizes. I&#8217;ve also included a means to turn debug information on/off so debug statements are not included in builds targeted as final releases.<br />
<span id="more-2293"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  </span>
<span style="color: #11740a; font-style: italic;">// Debug</span>
<span style="color: #11740a; font-style: italic;">//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span>
<span style="color: #6e371a;">#define DEBUG_ON  // Toggle to DEBUG_OFF to hide all debug code </span>
&nbsp;
<span style="color: #6e371a;">#ifdef DEBUG_ON</span>
  <span style="color: #6e371a;">#define debug(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);</span>
<span style="color: #6e371a;">#else</span>
  <span style="color: #6e371a;">#define debug(format, ...) </span>
<span style="color: #6e371a;">#endif</span>
&nbsp;
<span style="color: #6e371a;">#define debugRect(rect) debug(@&quot;%s x:%.4f, y:%.4f, w:%.4f, h%.4f&quot;, #rect,</span>
    rect.origin.x, rect.origin.y, rect.size.width, rect.size.height<span style="color: #002200;">&#41;</span>
<span style="color: #6e371a;">#define debugSize(size) debug(@&quot;%s w:%.4f, h:%.4f&quot;, #size, size.width, size.height)</span>
<span style="color: #6e371a;">#define debugPoint(point) debug(@&quot;%s x:%.4f, y:%.4f&quot;, #pt, pt.x, pt.y)</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/debugging-macros.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Write Debug Output to a File</title>
		<link>http://iPhoneDeveloperTips.com/general/write-debug-output-to-a-file.html</link>
		<comments>http://iPhoneDeveloperTips.com/general/write-debug-output-to-a-file.html#comments</comments>
		<pubDate>Tue, 23 Dec 2008 10:05:44 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1386</guid>
		<description><![CDATA[NSLog definitely has its place in debugging. In a previous post I shared a version I wrote that skips displaying the date and object information, you can read more about the debug command I use on a regular basis here. At some point, capturing debug statements to a file (versus the console) can be helpful [...]]]></description>
			<content:encoded><![CDATA[<p>NSLog definitely has its place in debugging. In a previous post I shared a version I wrote that skips displaying the date and object information, you can read more about the debug command I use on a regular basis <a href="http://iphonedevelopertips.com/debugging/yet-another-debug-output-nslog-replacement.html">here</a>.<br />
<span id="more-1386"></span></p>
<p>At some point, capturing debug statements to a file (versus the console) can be helpful to track down bugs. The good news is that you can continue to use NSLog (and/or the debug command from the previous post) by simply making a change to where stderr is directed. </p>
<p>For example, to re-direct stderr to a file &#8220;debug.txt&#8221; off the root of the system drive, follow these steps:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Create OS specific path</span>
<span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>path <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/debug.txt&quot;</span> fileSystemRepresentation<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Specify stderr writes to a file (truncating contents first) </span>
<span style="color: #a61390;">freopen</span><span style="color: #002200;">&#40;</span>path, <span style="color: #bf1d1a;">&quot;w&quot;</span>, <span style="color: #a61390;">stderr</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Each time you run your application, the contents of the file &#8220;debug.txt&#8221; will be truncated and all output written to the file. If you would like to keep a running tally of debug output across invocations of the application, you can change &#8216;freopen&#8217; to append contents as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Specify stderr appends to a file</span>
<span style="color: #a61390;">freopen</span><span style="color: #002200;">&#40;</span>path, <span style="color: #bf1d1a;">&quot;a&quot;</span>, <span style="color: #a61390;">stderr</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/general/write-debug-output-to-a-file.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yet Another Debug Output (NSLog Replacement)</title>
		<link>http://iPhoneDeveloperTips.com/debugging/yet-another-debug-output-nslog-replacement.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/yet-another-debug-output-nslog-replacement.html#comments</comments>
		<pubDate>Tue, 18 Nov 2008 20:05:55 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1040</guid>
		<description><![CDATA[Although NSlog is convenient for outputting messages to the console, I tire of the date/time and object information that it prints. As an alternative, one can craft a macro that uses CFShow, which outputs Core Foundation objects to stderr. CFShow uses callbacks to objects to display their descriptions, which allows one to use &#8220;%@&#8221; like [...]]]></description>
			<content:encoded><![CDATA[<p>Although NSlog is convenient for outputting messages to the console, I tire of the date/time and object information that it prints. </p>
<p>As an alternative, one can craft a macro that uses <strong>CFShow</strong>, which outputs Core Foundation objects to stderr. CFShow uses callbacks to objects to display their descriptions, which allows one to use &#8220;%@&#8221; like you would when calling <strong>NSLog</strong>. </p>
<p>I often include the following debug definition as part of my projects:<br />
<span id="more-1040"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define debug(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);</span></pre></div></div>

<p>A few short examples follow showing the difference in output between <strong>NSLog</strong> and the debug macro shown above.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span> point
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">int</span> x;
  <span style="color: #a61390;">int</span> y;
<span style="color: #002200;">&#125;</span>;
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">struct</span> point POINT;
&nbsp;
POINT topLeft;
topLeft.x <span style="color: #002200;">=</span> <span style="color: #2400d9;">15</span>, topLeft.y <span style="color: #002200;">=</span> <span style="color: #2400d9;">20</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;x: %d y: %d&quot;</span>, topLeft.x, topLeft.y<span style="color: #002200;">&#41;</span>;
debug<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;x: %d y: %d&quot;</span>, topLeft.x, topLeft.y<span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>The output will now look as follows:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2008/11/debug-1.png"/></p>
<p>Here&#8217;s another example, this time passing in an object:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>today <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSDateFormatter</span> <span style="color: #002200;">*</span>dateFormat <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDateFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>dateFormat setDateFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;EEEE MMMM d, YYYY&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>dateString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dateFormat stringFromDate<span style="color: #002200;">:</span>today<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>dateFormat release<span style="color: #002200;">&#93;</span>;
&nbsp;
...
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Date: %@&quot;</span>, dateString<span style="color: #002200;">&#41;</span>;
debug<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Date: %@&quot;</span>, dateString<span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>And the corresponding output:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2008/11/debug-2.png"/></p>
<p>Ahhh, much better.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/yet-another-debug-output-nslog-replacement.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
