<?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"
	>

<channel>
	<title>No Business Naming Things &#187; cocoa</title>
	<atom:link href="http://pookzilla.net/wp/tags/cocoa/feed/" rel="self" type="application/rss+xml" />
	<link>http://pookzilla.net/wp</link>
	<description>Where sassy women wear jaunty hats.</description>
	<pubDate>Tue, 18 Nov 2008 17:27:55 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>You Heard The Man, Blow Your Brains Out!</title>
		<link>http://pookzilla.net/wp/2007/11/you-heard-man-blow-your-brains-out/</link>
		<comments>http://pookzilla.net/wp/2007/11/you-heard-man-blow-your-brains-out/#comments</comments>
		<pubDate>Tue, 20 Nov 2007 15:29:00 +0000</pubDate>
		<dc:creator>pookzilla</dc:creator>
		
		<category><![CDATA[Eclipse]]></category>

		<category><![CDATA[cocoa]]></category>

		<guid isPermaLink="false">http://pookzilla.net/wp/2007/11/20/you-heard-the-man-blow-your-brains-out/</guid>
		<description><![CDATA[So you&#8217;ve read Steve&#8217;s blog and you&#8217;re all excited to help out with the Cocoa port (as you should be).  But then you grab the code and you&#8217;re worried.  Just how do you do anything with this?  This is all so new and rough.  You&#8217;re plodding along, kicking the tires, and [...]]]></description>
			<content:encoded><![CDATA[<p>So you&#8217;ve read Steve&#8217;s <a href="http://inside-swt.blogspot.com/2007/11/ok-community-blow-your-brains-out.html">blog</a> and you&#8217;re all excited to help out with the Cocoa port (as you should be).  But then you grab the code and you&#8217;re worried.  Just how do you do anything with this?  This is all so new and rough.  You&#8217;re plodding along, kicking the tires, and suddenly you&#8217;re assaulted by a segfault in the native library.  Your pant burst into flame, your dog gives birth to kittens, and you start singing soprano.  Where do you start?  How do you track this issue down?</p>
<p>There are going to be crashes.  When you come across one that smells like a segfault here&#8217;s a simple (if somewhat laborious) method to track it down.</p>
<ol>
<li> open NSObject and put the following line at the beginning of release():
<div class="codesnip-container" >System.out.println(&#8221;Release: &#8221; + toString());</div>
</li>
<li> launch and make the crash happen</li>
<li> In your Console view you should see reams and reams of lines like this:
<div class="codesnip-container" >&#8230;<br />
Release: org.eclipse.swt.internal.cocoa.NSBezierPath@dd4cd3<br />
Release: org.eclipse.swt.internal.cocoa.NSAttributedString@deb323<br />
Release: org.eclipse.swt.internal.cocoa.NSBezierPath@c2ee77</div>
</li>
<li> Starting from the bottom up locate each subclass of NSObject (such as NSBezierPath) and either override it&#8217;s
<div class="codesnip-container" >release()</div>
<p> method such that it&#8217;s empty or alter the existing
<div class="codesnip-container" >release()</div>
<p> method so that it does nothing.</li>
<li> repeat steps 2-4 with each successive subclass you encounter until the crash stops happening.  Something pertaining to the last class you changed is probably your culpret.</li>
<li> restore the
<div class="codesnip-container" >release()</div>
<p> method on the subclass to its original form so that the crash happens again.  Remove the
<div class="codesnip-container" >System.out.println</div>
<p> as well if you no longer feel it&#8217;s helpful to you.</li>
<li> find all callers to
<div class="codesnip-container" >release()</div>
<p> on the subclass.</li>
<li> from here it gets fuzzy.  You&#8217;ll need to start commenting out calls to
<div class="codesnip-container" >release()</div>
<p> and see if any particular disposal of the object is responsible.  Perhaps
<div class="codesnip-container" >release()</div>
<p> is called but the object is still held in SWT and later used for another purpose. If commenting out any particular
<div class="codesnip-container" >release()</div>
<p> call doesn&#8217;t stop the crash from happening you start looking at how the object is being retained.  If we&#8217;re using an object from another structure are we calling
<div class="codesnip-container" >retain()</div>
<p> or
<div class="codesnip-container" >copy()</div>
<p> on it?  We should never be
<div class="codesnip-container" >release()</div>
<p>-ing anything we haven&#8217;t previously
<div class="codesnip-container" >retain()</div>
<p>&#8216;d (explicitly or implicitly)</li>
</ol>
<p>Here&#8217;s how I applied this technique to find the crash Steve alluded to that was preventing Eclipse from coming up.  I added my System.out statement to
<div class="codesnip-container" >NSObject.release()</div>
<p> and started Eclipse.  It crashed and the last thing to be released was
<div class="codesnip-container" >NSAutoReleasePool</div>
<p>.  I added an empty
<div class="codesnip-container" >release()</div>
<p> method to this class and tried to launch again.  This time it crashed on
<div class="codesnip-container" >NSBezierPath</div>
<p>.  I added a no-op
<div class="codesnip-container" >release()</div>
<p> to
<div class="codesnip-container" >NSBezierPath</div>
<p> and tried again.  No crash!  I then reverted
<div class="codesnip-container" >NSAutoReleasePool</div>
<p> and turned my attention towards callers to
<div class="codesnip-container" >NSBezierPath.release()</div>
<p>.</p>
<p>I found two of interest in
<div class="codesnip-container" >GC</div>
<p> and another in
<div class="codesnip-container" >Path</div>
<p>. I decided to look at
<div class="codesnip-container" >GC</div>
<p> first because it seemed more interesting than
<div class="codesnip-container" >Path</div>
<p>.  I commented out one of the releases in
<div class="codesnip-container" >GC.setClipping()</div>
<p> and launched only to find that the crash persisted.  I then commented out the one in
<div class="codesnip-container" >GC.dispose()</div>
<p> and still the crash persisted.  This one was going to be a bit more difficult than a simple disposal.  I turned my attention towards all the places we set the
<div class="codesnip-container" >data.clipPath</div>
<p> member (the member that contained a reference to an
<div class="codesnip-container" >NSBezierPath</div>
<p>).  I discovered two places where we set the value of
<div class="codesnip-container" >data.clipPath</div>
<p>: we
<div class="codesnip-container" >null</div>
<p> it out in
<div class="codesnip-container" >GC.dispose()</div>
<p> after calling
<div class="codesnip-container" >release()</div>
<p> on it and we<br />
both
<div class="codesnip-container" >null</div>
<p> it out and assign it in
<div class="codesnip-container" >setClipping</div>
<p>.  I decide to look in
<div class="codesnip-container" >setClipping()</div>
<p> first because the
<div class="codesnip-container" >dispose()</div>
<p> looks innocuous enough.</p>
<p>In
<div class="codesnip-container" >setClipping</div>
<p> we&#8217;re releasing the previous value of
<div class="codesnip-container" >data.clipPath</div>
<p>, if any.  We then go and assign the supplied
<div class="codesnip-container" >NSBezierPath</div>
<p> to the
<div class="codesnip-container" >data.clipPath</div>
<p> member.  Hmm.  Interesting.  We&#8217;re releasing an object we haven&#8217;t retained in this method.  We must be retaining it elsewhere, right?  Let&#8217;s look.  There are four callers to the
<div class="codesnip-container" >setClipping()</div>
<p> method that takes an
<div class="codesnip-container" >NSBezierPath</div>
<p>.  The first, taking four integer arguments, constructs and retains a new
<div class="codesnip-container" >NSBezierPath</div>
<p> so that isn&#8217;t our culprit.  The second, taking a
<div class="codesnip-container" >Path</div>
<p>, creates a
<div class="codesnip-container" >copy()</div>
<p> of the path
<div class="codesnip-container" >NSBezierPath</div>
<p> instance (which is <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/copy">implicitly
<div class="codesnip-container" >retain()</div>
<p>&#8216;d</a>).  This one is probably good too.  Next up at bat is
<div class="codesnip-container" >setClipping</div>
<p> that takes a
<div class="codesnip-container" >Region</div>
<p> as input.  This one looks suspicious!  Here we&#8217;re passing in the
<div class="codesnip-container" >NSBezierPath</div>
<p> of the
<div class="codesnip-container" >Region</div>
<p> object without retaining it.  Decrementing the reference count of an object you haven&#8217;t previously increased the reference for is a surefire way of causing grief.  If we simply add a
<div class="codesnip-container" >retain()</div>
<p> or
<div class="codesnip-container" >copy()</div>
<p> call to the
<div class="codesnip-container" >NSBezierPath</div>
<p> object we&#8217;re getting from
<div class="codesnip-container" >Region.getPath()</div>
<p> the crash should go away.  And lo it does.</p>
<p>This virgin Cocoa port represents an incredibly awesome opportunity for the community to shine.  People have been crying for a Cocoa port for years now and now there&#8217;s an opportunity for industrious contributors to grab the bull by the horns and make it happen.  The rewards (in terms of good will alone) are enormous.  The SWT team has done a remarkable job bootstrapping the effort to get this port underway and it would be a real shame if the community didn&#8217;t jump on board and make it happen.  Long term we absolutely <span style="font-weight: bold">NEED</span> Cocoa for continued existence on the Mac - Carbons days are numbered.  If you&#8217;re an Eclipse user on the Mac you owe it to yourself and the community to participate in this as best you can.  If you don&#8217;t feel you&#8217;re able to submit patches then well-investigated bug reports will do.  This is a huge amount of work and every little bit helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://pookzilla.net/wp/2007/11/you-heard-man-blow-your-brains-out/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
