<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
   <channel>
      <title>Nat!&apos;s Web Journal</title>
      <link>http://www.mulle-kybernetik.com/weblog/</link>
      <description></description>
      <language>en</language>
      <copyright>Copyright 2010</copyright>
      <lastBuildDate>Wed, 01 Sep 2010 23:48:10 +0100</lastBuildDate>
      <generator>http://www.sixapart.com/movabletype/</generator>
      <docs>http://blogs.law.harvard.edu/tech/rss</docs> 

      
      <item>
         <title>When in -mouseEntered: don&apos;t use NSPointInRect for checking which tracking area is hit</title>
         <description><![CDATA[Do not assume that the mouse location of any mouse tracking <tt>NSEvent</tt> is necessarily inside or outside of the tracking rectangle as specified by the <tt>NSTrackingArea</tt>. Use the <tt>-trackingArea</tt> method of <tt>NSEvent</tt> instead of <tt>NSPointInRect</tt> to check, which trackingArea has triggered the event.
<p>

<H3>Bad code</h3> 

<blockquote><table bgcolor=0 border=0 cellspacing=1 cellpadding=3><tr bgcolor="#FCF8F8"><td>

<pre>- (void) syncInsideWithEvent:(NSEvent *) event
{
   NSPoint       point;
   unsigned int  i;
   
   point = [self convertPoint:[event locationInWindow] 
                     fromView:nil];
   for( i = 0; i &lt; N_AREAS; i++)
      inside_[ i] = NSPointInRect( point, area_[ i]);

   [self setNeedsDisplay:YES];
}


- (void) mouseEntered:(NSEvent *) event 
{ 
   [self syncInsideWithEvent:event];
}


- (void) mouseExited:(NSEvent *) event 
{ 
   [self syncInsideWithEvent:event];
}
</pre></td></tr></table></blockquote>


<H3>Better code</h3> 

<blockquote><table bgcolor=0 border=0 cellspacing=1 cellpadding=3><tr bgcolor="#F8FCF8"><td>

<pre>- (void) syncInsideWithEvent:(NSEvent *) event
                      invert:(BOOL) invert
{
   NSTrackingArea   *hit;
   NSTrackingArea   *area;
   unsigned int     i;
   
   hit = [event trackingArea];
   
   for( i = 0; i &lt; N_AREAS; i++)
   {
      area        = [[self trackingAreas] objectAtIndex:i];
      inside_[ i] = (area == hit) ^ invert;
   }
   [self setNeedsDisplay:YES];
}


- (void) mouseEntered:(NSEvent *) event
{
   [self syncInsideWithEvent:event
                      invert:NO];
}


- (void) mouseExited:(NSEvent *) event
{
   [self syncInsideWithEvent:event
                      invert:YES];
}
</pre></td></tr></table></blockquote>
<br>

]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2010/09/when_in_-mouseentered_dont_use.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2010/09/when_in_-mouseentered_dont_use.html</guid>
        
        
         <pubDate>Wed, 01 Sep 2010 23:48:10 +0100</pubDate>
      </item>
      
      <item>
         <title>MulleSybaseEOAdaptor 2.12 released</title>
         <description><![CDATA[Although I am not actively using Sybase anymore (unfortunately), there are occasional tweaks and improvements made to the adaptor. So version <a href="http://www.mulle-kybernetik.com/software/MulleSybaseEOAdaptor">2.12</a> should be better than 2.7 in any case, if not please tell me.
I need to port the code to <b>Intel</b> some time, but I am occupied with other stuff
<p>
Here are the releasenotes of the unpublished versions.

<h4>Version 2.12</h4>

<ol>
<li>Changed hash algorithm of mulleSybaseRowDictionary optimization done in 2.7 for even better performance
</ol>

<h4>Version 2.11</h4>

<ol>
<li> Reverted a supposedly harmless change in datetime handling since there were troubles in GMT 
    apparently.
</ol>

<h4>Version 2.10</h4>

<ol>
<li> Use slower but more compatible code to produce the inital SELECT statement for text/image writes
<li> Experimental fix for writetext, when size is evenly divisible by 512
</ol>

<h4>Version 2.9</h4>

<ol>
<li> I am really pedantic on +formatValue not being nil. It shouldn't happen when using MulleEOF and
you should be passing in [EONull null] anyway
<li> Removed a leak involving error messages
<li> Fixed a special Sybase type handling of LONGCHAR types
<li> Avoided a leak when returning factory produced values
</ol>


<h4>Version 2.8</h4>

<ol>
<li> You may pass in "nil" as a value for -[EOAdaptor fetchedValueForValue:attribute:]
<li> Finally in version 2.8 "expressionClass" will not be overridden, but "defaultExpressionClass",
which is correct.
<li> If an image attibute has value null, the adaptor will not write a serialized [EONull null] into the DB.
<li> Image/text is written with a simpler qualifier, that is just qualifying the primary key.
<li> -[MulleSybaseSQLExpression primaryKeyConstraintStatementsForEntityGroup]produces
proper SQL code now
<li> Versioning information should be correct (thx to patrick@onestep.co.uk for noting that).
<li> Fixed Void and In/out parameters sql generation to produce syntactically correct SQL
<li> JDBC compatibility port number hack
<li> Fixed creation of primary key constraints generation (or rather their declaration, the statement issued
is not a true constraint, which I might fix with MulleEOF 2.0)
<li> If _debug flag is set, the adaptor now writes the number of bytes read (along with the SQL) in
completeFetch
</ol>
]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2010/08/mullesybaseeoadaptor_212_relea.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2010/08/mullesybaseeoadaptor_212_relea.html</guid>
        
        
         <pubDate>Mon, 02 Aug 2010 12:48:20 +0100</pubDate>
      </item>
      
      <item>
         <title>EXC_BAD_ACCESS and no stack trace ==  more quality time lost in Xcode</title>
         <description><![CDATA[<H4>DISCLAIMER: THIS IS FOR i386 ONLY</h4>
And as things are wont to do, Xcode debugging just went from bad to worse for me. To figure out some other problems (see previous posts) I enabled some debug code, which led to this crasher in middle of nowhere^h^h^h^h^h^h^h<tt>objc_msgSend</tt>. There is this tendency of debug code to create more bugs and henceforth more debug code, but I disgress...
<p>
With no clue in the console log and no stack trace (see picture) I was stumped.
<p>
 <img src="http://www.mulle-kybernetik.com/weblog/pix/middle-of-nowhere.png" height="359" width="447" align="" border="0" hspace="0" vspace="0" alt="" title="Xcode dazed and confused " longdesc="" />
<p>
<p>Obviously the crash occurs at <tt>0x986ceed7</tt>, where you see the red instruction pointer arrow.
<p>
<H3>Recovering the Stacktrace</h3>
You can help Xcode along a little ways to display the correct stack trace, and only god and apple know, why it can't do this on its own, but I guess syntax coloring is way more important...
<p>
Move the instruction pointer to a <tt>ret</tt> instruction. In the screenshot the <b>address of a known <tt>ret</tt> instruction is marked green.</b> If you see a different hexadecimal number in your disassembly there, by all means use that. Now go into the gdb debugger console to recover the stack trace:
<blockquote><pre>(gdb) set $eip = 0x986cef35
(gdb) stepi
(gdb) where
#0  0x00000000 in ?? ()
#1  0x9842e5a4 in _NSDescriptionWithLocaleFunc ()
#2  0x92b04322 in _CFStringAppendFormatAndArgumentsAux ()
#3  0x92b036a9 in _CFStringCreateWithFormatAndArgumentsAux ()
#4  0x92babf2e in _CFLogvEx ()
#5  0x984bf7e8 in NSLogv ()
#6  0x984bf757 in NSLog ()
...</pre>
</blockquote><p>
Voil&#224;! Or Viola as it's commonly misspelled.
]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2010/07/exc_bad_access_and_no_stack_tr.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2010/07/exc_bad_access_and_no_stack_tr.html</guid>
        
        
         <pubDate>Wed, 14 Jul 2010 17:25:20 +0100</pubDate>
      </item>
      
      <item>
         <title>Betcha can&apos;t debug that</title>
         <description><![CDATA[Along the lines of <a href="http://www.youtube.com/watch?v=o5xYURdXVN8">those guitar videos</a> here is an Xcode Project, that crashes in the Xcode debugger but not stand alone. I bet you can't debug that!
<p>
<blockquote>
<table border=1 cellspacing=0 cellpadding=4><tr><td><a href="http://www.mulle-kybernetik.com/weblog/files/MoreTroubleInXcode.zip">MoreTroubleInXcode.zip</a></td></tr></table</blockquote>
<p>
The expected output of the debugger is:
<blockquote><tt>Running...<br>
memory_pressure: 0memory_pressure: 02010-07-13 21:51:44.114 MoreTroubleInXcode[13281:a0f] I don't blame you.<br>
Program received signal:  "SIGABRT".<br>
sharedlibrary apply-load-rules all<br>
Cannot access memory at address 0x0<br>
Cannot access memory at address 0x0<br>
(gdb)</tt></blockquote>]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2010/07/betcha_cant_debug_that.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2010/07/betcha_cant_debug_that.html</guid>
        
        
         <pubDate>Tue, 13 Jul 2010 20:43:10 +0100</pubDate>
      </item>
      
      <item>
         <title>(Buggy ?) Instruments tries to trick me</title>
         <description><![CDATA[Here is a small Xcode test project that will successfully run standalone or in the debugger, but will crash when run with Instruments. 

<p>
<blockquote>
<table border=1 cellspacing=0 cellpadding=4><tr><td><a href="http://www.mulle-kybernetik.com/weblog/files/SomethingBadHappened.zip">SomethingBadHappened.zip</a></td></tr></table</blockquote>
<p>

(In Xcode use the "Run" menu, choose "Run with Performance Tool"  and select "Leaks").
What this project is doing, is overloading a category in Foundation with a method, that is itself stored in a Framework. In a condensed form it looks like this.

<blockquote><table bgcolor=0 border=0 cellspacing=1 cellpadding=0><tr bgcolor="#F8F8F8"><td><pre>// defined in a framework 
@implementation NSObject( NSUnpublishedEOFDebug)

- (void) addObject:(id) obj 
toBothSidesOfRelationshipWithKey:(NSString *) key
{
   NSLog( @"%@ %s: %@ %@", 
          self, __PRETTY_FUNCTION__, obj, key);
   NSLog( @"Denied!");
}

@end
</pre></td></tr></table></blockquote>

and 

<blockquote><table bgcolor=0 border=0 cellspacing=1 cellpadding=0><tr bgcolor="#F8F8F8"><td><pre>// defined in a tool, linking to the framework
int main (int argc, const char * argv[]) 
{
   [@"foo" addObject:@"bar" 
toBothSidesOfRelationshipWithKey:@"baz"];
}</pre></td></tr></table></blockquote>

And this is what you will see, if you are using Xcode 3.2.2.
<p>
<table border=0 cellspacing=0><tr bgcolor="#EFEFEF"><th>Normal run</th></tr>
<tr bgcolor="#EFDFEF"><td>
<blockquote><tt>+[NSObject( NSUnpublishedEOFDebug) load]<br>
foo -[NSObject(NSUnpublishedEOFDebug) addObject:toBothSidesOfRelationshipWithKey:]: bar baz<br>
Denied!<br>
</tt></blockquote></td></tr></table>

<table border=0 cellspacing=0><tr bgcolor="#EFEFEF"><th>Instruments run</th></tr>
<tr bgcolor="#DFEFEF"><td>
<blockquote><tt>+[NSObject( NSUnpublishedEOFDebug) load]<br>
*** WARNING: -[NSObject(NSKeyValueCoding) takeValue:forKey:] is deprecated. It will be removed in a future release and should no longer be used.<br>
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[&lt;NSCFString 0x2024&gt; takeValue:forKey:]: attempt to assign value to unknown key: 'baz'.<br>
This class does not have an instance variable of the name baz or _baz, nor a method of the name setBaz, or _setBaz'<br>
*** Call stack at first throw:<br>
(<br>
0   CoreFoundation                      0x9440abba __raiseError + 410<br>
1   libobjc.A.dylib                     0x94f4d509 objc_exception_throw + 56<br>
2   CoreFoundation                      0x944559f1 -[NSException raise] + 17<br>
3   Foundation                          0x9833a179 _raiseKeyValueException + 234<br>
4   SomethingBadHappened                0x00001ef7 main + 125<br>
5   SomethingBadHappened                0x00001e71 start + 53<br>
)<br>
&lt;End of Run&gt;<br></tt></blockquote></td></tr></table>

<p>
<h3>Some Thoughts</h3>
<ul>
<li>Does not crash with Shark
<li>Yes, Foundation still has some old EOF methods contained in a category called <tt>NSUnpublishedEOF</tt>.
<li>If the category is not part of the test framework, but part of the tool. There is no such problem.
<li>Since, the test framework is dependent on Foundation, Foundation must be loaded first.
<li>It's as if by a accident Foundation was loaded again, and it's categories override the test framework categories.
</ul>]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2010/07/buggy_instruments_tries_to_tri.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2010/07/buggy_instruments_tries_to_tri.html</guid>
        
        
         <pubDate>Tue, 13 Jul 2010 00:52:11 +0100</pubDate>
      </item>
      
      <item>
         <title>retain/release one more time + data</title>
         <description><![CDATA[<i>In <a href="http://www.mulle-kybernetik.com/weblog/2010/06/retainrelease_one_more_time_ma.html">the previous entry</a> I made an assumption about the lifetime of objects in a Objective-C program (See: <strong>Probabilities</strong>). This entry will provide some data, that make this assumption more plausible and to refine it.</i>
<p>
So here are the results of two experiments. I wrote some code, that measured the number of <tt>release</tt>s on <tt>NSObject</tt> based objects. I then dropped that into the Apple developer examples <strong>Sketch</strong> and <strong>TextEdit</strong>. I ran both for a short while I did some manipulations like loading files, editing, copy/pasting et.c and then on <tt>Quit</tt>, my code collected and printed the statistics data. 
<p>
<blockquote><table border=1 cellspacing=0 cellpadding=2>
<tr bgcolor="#EEEEEE"><th>Sketch</th><th>TextEdit</th></tr>
<tr><td>Download <a href="http://www.mulle-kybernetik.com/weblog/pix/Sketch.txt">Sketch.txt</a></td>
<td>Download <a href="http://www.mulle-kybernetik.com/weblog/pix/TextEdit.txt">TextEdit.txt</a></td></tr>
<tr><td><pre>total object count = 26065

1	14016	0.537733
2	9598	0.368233
3	1090	0.041819
4	412	0.015807
5	145	0.005563
6	46	0.001765
7	67	0.002570
8	44	0.001688
9	236	0.009054
10	36	0.001381
</pre></td>
<td><pre>total object count = 13652

1	7056	0,516847
2	4420	0,323762
3	1033	0,075667
4	241	0,017653
5	167	0,012233
6	11	0,000806
7	128	0,009376
8	62	0,004541
9	165	0,012086
10	38	0,002783</pre></td></tr>
</table></blockquote>

<i>The first column has the <tt>release</tt> count number. The second column shows the number of objects. Finally the  third row contains the weight of the <tt>release</tt> count number (#objects / total). In this table I only show the first 10 release count statistics, to keep it simple.</i>
<p>
So for example for <strong>Sketch</strong>, there were <tt>1090</tt> objects that got <tt>3</tt> <tt>release</tt>s before they <tt>dealloc</tt>ed. (This implies they were <tt>retain</tt>ed twice). So because <tt>26065</tt> objects existed during this run of <strong>Sketch</strong>, the probability that an object would die with <tt>3 </tt><tt>release</tt>s is 0.041819.

<h3>What it means</h3>
Clearly the assumption was much too conservative. Instead of a gaussian-ish distribution, the distribution looks more like a logarithmic curve. In these scenarios 90% of all objects never get more than three <tt>release</tt>s. Half of them are never <tt>retain</tt>ed even once.
<p>
Let's redo the math for <strong>Sketch</strong>, with the new numbers, collapsing the <tt>release</tt>s from 6 to the maximum <tt>release</tt> count into 6 to keep it simple and recognizable:

<blockquote><table border=1 cellspacing=0 cellpadding=2>
<tr><th># releases (n)</th><th>percentage</th><th>factor</th></tr>
<tr><td>1</td><td>53%</td><td> 0.53 </td></tr>
<tr><td>2</td><td>37%</td><td> 0.37</td></tr>
<tr><td>3</td><td>4%</td><td> 0.04 </td></tr>
<tr><td>4</td><td>2%</td><td> 0.02 </td></tr>
<tr><td>5</td><td>1%</td><td> 0.01 </td></tr>
<tr><td>6</td><td>3%</td><td> 0.03 </td></tr>
</table></blockquote>
<p>

<blockquote><table bgcolor="#EFFFFF" border=0 cellspacing=0 cellpadding=0><tr><td><pre><pre>= factor( n) * tM * n
= 0.53 * 1 + 0.37 * 2 + 0.04 * 3 + 0.02 * 4 + 0.01 * 5 + 0.03 * 6
= 0.53 + 0.74 + 0.12 + 0.08 + 0.05 + 0.18 
= 1.7</pre></td></tr></table></blockquote>

<p>

<blockquote><table bgcolor="#FFEFEF" border=0 cellspacing=0 cellpadding=0><tr><td><pre>= factor( n) *  (tS * n + tM * (n - 1))
= factor( n) * (4/3 * n - 1)

= 0.53*0.33+0.37*1.66+0.04*3+0.02*4.33+0.01*5.66+0.03*7
= 0.17 + 0.61 + 0.12 + 0.087 + 0.057 + 0.21 
= 1,25</pre></td></tr></table></blockquote>

<H3>Final Words</h3>
"old" is now estimated to be <tt>(1.7 - 1.25) * 100 / 1.25 = 36 %</tt> slower than "new" in Sketch, if <strong>tS</strong> is <strong>1/3</strong> of <strong>tM</strong>. The previous estimate was 13.7%.




]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2010/06/retainrelease_one_more_time_da.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2010/06/retainrelease_one_more_time_da.html</guid>
        
        
         <pubDate>Sat, 12 Jun 2010 01:12:36 +0100</pubDate>
      </item>
      
      <item>
         <title>retain/release one more time + math</title>
         <description><![CDATA[<i>This adds some easy math as a background to the <a href="http://www.mulle-kybernetik.com/weblog/2010/05/retainrelease_one_more_time.html">preceeding log entry</a>. It doesn't prove anything about the worthwhileness of the "new" version.</i>
<p>
Looking at the original "old" code, what is underlayed with the bluish tint, is the actual "algorithmic" part. Let's call this part <strong>M</strong> for multithreaded and lets name the time spent executing this part of the method <strong>tM</strong>.
<blockquote><pre>- (void) release
{
<table bgcolor="#EFFFFF" border=0 cellspacing=0 cellpadding=0><tr><td>   if( MulleAtomicDecrement( &#38;refCountMinusOne_) == -1) // M
</td></tr></table>      [self dealloc];
}</pre></blockquote>

Let's call the added code in the "new" version the, code with the red underlay <strong>S</strong>  for singlethreaded and its time  <strong>tS</strong>. We will ignore the time spent in calling <tt>dealloc</tt> since that is outside the scope of current interest.

<blockquote><pre>- (void) release
{
<table bgcolor="#FFEFEF" border=0 cellspacing=0 cellpadding=0><tr><td>   if( ! refCountMinusOne_)  // S 
</td></tr></table>   {
      [self dealloc];
      return;
   }
</td></tr></table>
<table bgcolor="#EFFFFF" border=0 cellspacing=0 cellpadding=0><tr><td>   if( MulleAtomicDecrement( &#38;refCountMinusOne_) == -1) // M
</td></tr></table>      [self dealloc];
}</pre></blockquote>

<H3>Simple Functions</H3>
Assume  an object is allocated and then <tt>release</tt>d, thereby deallocated. Assume there had been no intervening <tt>retain</tt>s and <tt>release</tt>s, like <tt>[[Foo new] release];</tt><p>This shall constitute the lifetime of the object and it's lifetime is measured in releases (or n). Therefore the lifetime of this object is 1. In this case the "old" code spends <strong>tM</strong> time and the "new" code <strong>tS</strong> time. <p>
If on the other hand the object had been <tt>retain</tt>ed and <tt>release</tt>d once in between (<tt>p = [[Foo new] retain]; [p release]; [p release]</tt>), there are now two <tt>release</tt>s. The lifetime for this instance is therefore <b>2</b> and the "old" code spends <strong>tM + tM</strong>, whereas the "new" code spends <strong>tS + tM + tS</strong>. Continuing this sequence we get the following table for a few <tt>retain/release</tt> cycles
<p>
<blockquote><table border=1 cellspacing=0 cellpadding=2>
<tr><th># releases (n)</th><th>old</th><th>new</th></tr>
<tr><td>1</td><td>tM</td><td> tS </td></tr>
<tr><td>2</td><td>tM + tM</td><td> tS + tS + tM </td></tr>
<tr><td>3</td><td>tM + tM + tM</td><td> tS + tS + tM + tS + tM </td></tr>
<tr><td>4</td><td>tM + tM + tM + tM</td><td> tS + tS + tM + tS + tM + tS + tM </td></tr>
</table></blockquote>
<p>
It should be easy to see, that this can be generalized to (shortening <i>number of <tt>release</tt>s</i>  to <strong>n</strong></i>):
<p>
<blockquote><table bgcolor="#EFFFFF" border=0 cellspacing=0 cellpadding=0><tr><td>old( n) = tM * n</td></tr></table></blockquote>
<blockquote><table bgcolor="#FFEFEF" border=0 cellspacing=0 cellpadding=0><tr><td>new( n) = tS * n + tM * (n - 1)</td></tr></table></blockquote>


<p>
With a little further math, we can put <strong>tS</strong> and <strong>tM</strong> as well as <strong>n</strong> into relation.

<blockquote><pre>old( n) = new( n)
tM * n = tS * n + tM * (n - 1)
tM * n = tS * n + tM * n - tM
    0 = tS * n - tM
    tM = tS * n
    tS = tM / n
     n = tM / tS
</pre></blockquote>

But what does that mean ? It gives the relationship, where using the "new" code is exactly the same as using the old "code" for a certain number of <tt>release</tt>s. So for example is we set <strong>tM</strong> to <strong>1</strong> and want to have a "new" code that is as fast as the "old" one up to a release count of <strong>3</strong>, we need <strong>tS</strong> to be <strong>1/3 tM</strong>.

<h3>Probabilities</h3>

To judge the improvement of the "new" code, we need a model of the probability that an object will be <tt>release</tt>d 'n' times over its lifetime. I don't have any hard data yet, so for illustration purposes I am assuming this guassian-ish kind of distribution, heavily favoring the lower release counts and ignoring any objects with a lifetime larger 6 (more releases than 6 simply don't happen in this simplified scenario).
The table lists the likelyhood that an object will have a lifetime for each possible lifetime. The percentages add up to 100. The factor simplies scales the likelyhood or probability to a number between 0 and 1. All Factors add to 1.
<p>
<blockquote><table border=1 cellspacing=0 cellpadding=2>
<tr><th># releases (n)</th><th>percentage</th><th>factor</th></tr>
<tr><td>1</td><td>20%</td><td> 0.2 </td></tr>
<tr><td>2</td><td>25%</td><td> 0.25</td></tr>
<tr><td>3</td><td>20%</td><td> 0.2 </td></tr>
<tr><td>4</td><td>20%</td><td> 0.2 </td></tr>
<tr><td>5</td><td>10%</td><td> 0.1 </td></tr>
<tr><td>6</td><td>5%</td><td> 0.05</td></tr>
</table></blockquote>
<p>
Then the average time spent for "old" will work out to, 

<blockquote><table bgcolor="#EFFFFF" border=0 cellspacing=0 cellpadding=0><tr><td>sum_old( 1, n) := factor( n) * old( n)</td></tr></table></blockquote>

where <strong>sum_old( 1, n)</strong> is supposed to be the summation over <strong>1</strong> to <strong>n</strong> of the right term of <tt><strong>:=</strong></tt>. The <strong>factor( n)</strong> is the value taken from the table. <strong>old( n)</strong> is the formula from above. This is simply a weighting function.
<p>

Assuming <strong>1</strong> for <strong>tM</strong>, then this would work out to

<blockquote><table bgcolor="#EFFFFF" border=0 cellspacing=0 cellpadding=0><tr><td><pre><pre>factor( n) * old( n) = factor( n) * tM * n
= factor( n) * 1 * n
= factor( n) * n
= 0.2 * 1 + 0.25 * 2 + 0.2 * 3 + 0.2 * 4 + 0.1 * 5 + 0.05 * 6
= 0.2 + 0.5 + 0.6 + 0.8 + 0.5 + 0.30 
= 2.9</pre></td></tr></table></blockquote>

<p>

The average time spent on "new" is likewise

<blockquote><table bgcolor="#FFEFEF" border=0 cellspacing=0 cellpadding=0><tr><td>sum_new( 1, n) := factor( n) * new( n)</td></tr></table></blockquote>

The same for <strong>tS</strong>, still assuming it is <strong>1/3 tM</strong>...

<blockquote><table bgcolor="#FFEFEF" border=0 cellspacing=0 cellpadding=0><tr><td><pre>factor( n) * new( n) = factor( n) *  (tS * n + tM * (n - 1))
= factor( n) * (1/3 tM * n + tM * (n - 1))
= factor( n) * tM (1/3 * n + (n  - 1))
= factor( n) * tM ( 4/3 n - 1)
= factor( n) * (4/3 * n - 1)

= 0.2*0.33+0.25*1.66+0.2*3+0.2*4.33+0.10*5.66+0.05*7
= 0.067 + 0.417 + 0.6 + 0.867 + 0.567 + 0.35 
= 2.55</pre></td></tr></table></blockquote>

So in this scenario with all these assumptions, "old" would be

(2.9 - 2.55) * 100 / 2.55 = 13,7 % slower

And that's what I meant, when I wrote, it could be a statistical win.

<H3>What to take home</H3>

The "new" code could be beneficial not just for objects with no intervening retains, but also for objects with very few intervening retains. It depends on how much quicker <strong>tS</strong> executes.

<p>
<H3>Stuff to do</H3>
<ul>
<li>find out the release statistics for a common Cocoa application
<li>measure <strong>tM</strong> and <strong>tS</strong> for MulleAtomic
</ul>


<H3>Stuff to remember</H3>
<ul>
<li>the possible change of <strong>tM</strong> by the preceeding execution of <strong>tS</strong> is reflected only in <strong>tS</strong> for simplicity
</ul>
]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2010/06/retainrelease_one_more_time_ma.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2010/06/retainrelease_one_more_time_ma.html</guid>
        
        
         <pubDate>Fri, 11 Jun 2010 01:10:34 +0100</pubDate>
      </item>
      
      <item>
         <title>retain/release one more time</title>
         <description><![CDATA[Once upon a time, on a website far, far away - no actually right here - and not so long ago, I wrote an <a href="http://www.mulle-kybernetik.com/artikel/Optimization/opti-4-atomic.html">article </a> about the subject of implementing <tt>retain/release</tt> safely and speedily:
<blockquote><b>Atomic Increment and Decrement</B><p>
Luckily the <tt>-retain</tt> and <tt>-release</tt> only needs to increment and decrement a variable safely. An increment or decrement is just such a read, modify, write operation that is a perfect match for an atomic operation.

<pre>- (id) retain
{
   MulleAtomicIncrement( &refCountMinusOne_);
   return( self);
}


- (void) release
{
   if( MulleAtomicDecrement( &refCountMinusOne_) == -1)
      [self dealloc];
}
</pre>
</blockquote>

Of course time doesn't stand still and maybe, possibly, depending on circumstances there might be a slightly faster way to implement this.
<p>
So let me show the code first and then explain it. While <tt>-retain</tt>'s implementation stays as is, <tt>release</tt> is changed to:

<pre>- (void) release
{
   if( ! refCountMinusOne_)  // single-threaded and done ?
   {
      [self dealloc];
      return;
   }

   // still multi-threaded, so do it safely
   if( MulleAtomicDecrement( &refCountMinusOne_) == -1)
      [self dealloc];
}
</pre>

<i>The reference count is stored, as the variable name indicates, with 1 subtracted. So an object starts out with a <tt>refCountMinusOne_</tt> value 0, meaning an actual reference count of 1.</i>
<p>
The first question of course would be "is this threadsafe ?". Assuming that a read to <tt>refCountMinusOne_</tt> is atomic, whenever another thread holds claim to the object, the value of <tt>referenceCountMinusOne_</tt> must be 1 or larger, since the currently executing thread also holds claim to at least one reference. Reading a 0 (an actual reference count of 1) in <tt>-release</tt> implies that the object is owned now solely by a single thread (the current thread).
<p>
The second question is, "why would this be better ?". That's something that needs to be tested on the given hardware, but my perception on atomic operations is, that they usually cause inter-CPU communication and synchronization overhead. Something that I expect to become of worse effect as core numbers increase. At least in my old article, there was a non-negligable difference.<br>
Avoiding atomic operations should therefore be a win.
<p>
So here's a scenario where this could be useful: Assume you have a subclass of <tt>NSArray</tt>, say <tt>MulleMutableArray</tt>, which allows you to initialize it with objects that are already retained, say <tt>-initWithRetainedObjects:count:</tt>.
Now fill this array with objects, that have been created with <tt>+new</tt>. If you then release this array, all its items will be sent a <tt>-release</tt> message and you would never have executed an atomic instruction.<br>This could also be useful for objects, that are immediately added to <tt>NSAutoreleasePools</tt> having never be retained.
<p>
<H3>Negative aspects</H3>
<ul>
<li>could fail on hardware, where int reads are non-atomic, like f.e. old 68000 multi-CPU (ha!) hardware - if I remember - where the databus was 16 bit and int size was 32 bit.
<li>will slow down "normal" code, that retains/releases. But on the bright side refCountMinusOne_ will be in the cache then pretty much guaranteed
<li>it's only a statistical advantage for certain scenarios
</ul>
]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2010/05/retainrelease_one_more_time.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2010/05/retainrelease_one_more_time.html</guid>
        
        
         <pubDate>Fri, 28 May 2010 15:32:28 +0100</pubDate>
      </item>
      
      <item>
         <title>Schilder f&#252;r die Plastikberge des White Water Flippers</title>
         <description><![CDATA[Mal 'nen etwas obskurer Eintrag in diesem Blog... Mit Hilfe von Gimp, Inkscape, OmniGraffle etwas Geschick und SEHR viel Zeit habe ich mir eine Druckvorlage f&#252;r Repros f&#252;r die Plastikberge vom <a href="http://www.ipdb.org/machine.cgi?id=2768">White Water Flipper</a> gebastelt.
<p>
Was am Ende doch mal wieder erstaunlich kompliziert war, war die Anpassung der Farbe. Ausgedruckt habe ich letztlich auf <b>EPSON Photo Quality Glossy Paper (S041126)</b> - hatte ich noch von meinem Tintendruckertagen &#252;brig - was nochmal einen ganz anderen Farbton produziert als normales Papier. Das krasseste jedoch war, wie sich die Farben durch Export und Import &#228;ndern. Im Original Graffle File (Colorsync ausgeschaltet) hat das Gr&#252;n einen HSV Wert von 157/42/77. Im exportierten PDF ist es dann 158/29/73. Woran das jetzt liegt ?
<p>
Und mit dem Colorswatch von Apple hatte ich auch diverse Probleme. Letztendlich liegt alles wohl daran, da&#223; manche Farben ger&#228;tabh&#228;ngig  und manche ger&#228;tunabh&#228;ngig sind und die Programme sich damit gegenseitig verwirren. Das ist leider den Farben aber nicht anzusehen. So war das ganze mit viel Fummelei verbunden.
<p>
Zu guter letzt die Dateien. Wenn's geht lieber aus dem OmniGraffle ausdrucken und die Druckoptionen beachten. Mit dem OKI C5600 und dem EPSON Papier wird es (IMO) perfekt: <a href="http://www.mulle-kybernetik.com/weblog/files/MulleWhiteWaterSchilder.zip"> MulleWhiteWaterSchilder.zip </a>
]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2010/04/schilder_fr_die_plastikberge_d.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2010/04/schilder_fr_die_plastikberge_d.html</guid>
        
        
         <pubDate>Sun, 18 Apr 2010 12:11:52 +0100</pubDate>
      </item>
      
      <item>
         <title>How I bricked my iTunes Server</title>
         <description><![CDATA[for a while...
<p>
As soon as it was available in Germany I bought a Sheeva Plug Computer (ca. EUR 100) and a 1 TB Imation external USB disk (ca. EUR 100).  My plan was to use this a fileserver, mainly as a DAAP iTunes Music server.
<p>
The Imation disk is great. It's covered with an absorber material, and I only hear it when I put my ear on it. It's also supposed to use very little energy.<br>The Sheeva Plug is not so great. The USB connections have too little friction, so it is extremely easy to lose the connection. I already had to format the USB disk twice, because it became unusable, when the USB cabled didn't connect properly any more... Also I think, it could be a little smaller. It's quite a beefy plug.
<p>
There are a lot of guides out there, that cover what to do with the Sheeva, once you get it. For a fileserver like mine, you only need three packages: <b>avahi-daemon</b> for Zeroconf, <b>mt-daapd</b> for Daap/iTunes and <b>samba</b> for filesharing. I also wanted <b>netatalk</b> for completeness sake.
<p>
I just want to write down a few notes, that I didn't see covered elsewhere on the web. There are a lot of good guides out there.
<p>
<ul>
<li><b>netatalk</b> - gave me some weird address family errors, but when I turned off the ATALKD daemon and just use AFPD daemon (see <tt>/etc/defult/netatalk/</tt>. It worked OK. I don't really use AFP now, I use samba, it seems so much faster.
<li><b>hostname</b> - append a .local to the name you come up with. Zeroconf will then be able to properly resolve it.
<li><b>fstab</b> - try to replace as much as possible of the <tt>/var</tt> hierarchy with <b>tmpfs</b> mounts. You will lose persistent logs, but that should reduce writes to the flash memory of the plug.
</ul>
As a final buff, I wanted to mount the root file system as read only. Truely an idiotic maneuvre in planning in execution, but here's what I did, no kidding...
<ol>
<li>Edited <tt>/etc/fstab</tt> to read <tt>rootfs / rootfs <b>ro</b> 00</tt> instead of <tt>rootfs / rootfs rw 0 0</tt>, hoping that this would just work
<li>Rebooted
<li>Checked with <b>mount</b> that it worked, it didn't (<tt>rootfs on / type rootfs (rw)</tt>)
<li>Decided to revert <tt>/etc/fstab</tt> to avoid confusion later in life
<li>Noticed  about three times in a row that saving the changed file didn't work.
<li>Examined <b>fstab</b> file permissions and my user <b>id</b> (root)
<li>Failed to see any reason, why it shouldn't work.
<li>Abandoned this problem and tried to figure out some other aesthetic problem in the boot process
<li>Figured out that I needed <b>strings</b> for that
<li>Tried <tt>apt-get install strings</tt>, knowing that it wouldn't work, which it didn't. Dimly rememberd something about "bin-utils"...
<li>Gave up on this, but noticed a warning that <b>apt-get</b> gave about not being able to use a read-only <tt>/var/lib/dpkg/lock</tt>.
<li>Figured, that probably <tt>/var/lib/dpkg</tt> was missing and needed creation
<li>Noticed, that this wasn't the case.
<li>Experienced a rare moment, that I wouldn't call lucid, but nevertheless ... The root filesystem was indeed mounted read only, and <tt>mount</tt> had lied to me!
<li>Moaned as I realized, that to install anything on the root file system I had to edit <b>fstab</b> and reboot. But everything on the root file system is read only now.
<li>Dealt with the pain, by writing this blog entry
<li>Rememberd the remount option <tt>mount -o remount,rw /</tt> ;)
</ol>
]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2010/02/how_i_bricked_my_itunes_server.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2010/02/how_i_bricked_my_itunes_server.html</guid>
        
        
         <pubDate>Sun, 21 Feb 2010 23:19:36 +0100</pubDate>
      </item>
      
      <item>
         <title>A mysterious and largely unknown gcc builtin function</title>
         <description><![CDATA[<pre>#include &lt;stdio.h&gt;

main()
{
   int  x;

   // \\\\\\\\\\\  ///////////// 
   // ||    POWERFUL MAGIC    ||
   // //////////   \\\\\\\\\\\\\
   x = __powerful_magic__( 1848);
   printf( "%d\n", x);

   return( 0);
}</pre>]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2010/01/a_mysterious_and_largely_unkno.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2010/01/a_mysterious_and_largely_unkno.html</guid>
        
        
         <pubDate>Tue, 12 Jan 2010 12:11:25 +0100</pubDate>
      </item>
      
      <item>
         <title>deprecation, deprecation, deprecation - Part 2</title>
         <description><![CDATA[So out of curiosity I figured I'd like to check the deprecation statistics.
<p>
Here's the unix command I used to get the statistics. I am collecting recursive all the header files inside an SDK directory, unique them by filename (which is a bug, but better than not doing it) and check for the AVAILABLE...BUT..DEPRECATED string. Then I massage the output a little, so I can easily put it in a table:
<p>
<tt>( for i in `find /Developer/SDKs/MacOSX10.4u.sdk/ -name "*.h" -print` ; do    file=`basename $i`;    echo "$file" "$i"; done | awk '{  files[ $1] = $2 } END { for ( x in files) print files[ x] }' | xargs egrep -H "AVAILABLE_MAC_OS_X_VERSION_10_._AND_LATER_BUT_DEPRECATED_IN_MAC_OS_X_VERSION_10_." ) | egrep -v "#define|#ifdef|#ifndef|\ \*\ .*AVA" | sed 's/.*\(AVAILABLE_.*_10_.\).*/\1/' | sort | uniq -c | sed 's/AVAILABLE_MAC_OS_X_VERSION_10_\(.*\)_AND_LATER_BUT_DEPRECATED_IN_MAC_OS_X_VERSION_10_\(.*\)/\1 \2/'</tt>
<p>
which exposes that the <tt>JavaVM.framework</tt> is apparently broken in the 10.6 SDK, but who uses Java anyway ;)<br>
<tt>egrep: /Developer/SDKs/MacOSX10.6.sdk//System/Library/Frameworks/JavaVM.framework/Versions/A/Headers/jvmdi.h: No such file or directory<br>
egrep: /Developer/SDKs/MacOSX10.6.sdk//System/Library/Frameworks/JavaVM.framework/Versions/A/Headers/jvmpi.h: No such file or directory<br>
</tt>
<p>
Anway here are the results:
<p>
<img src="http://www.mulle-kybernetik.com/weblog/pix/deprecated-chart.png" height="274" width="405" align="" border="0" hspace="0" vspace="0" alt="Mac OS X Deprecation Chart" title="" longdesc="" />
<p>
How to read this chart. On the left side is the released version. The bars show the number of symbols deprecated of previous OS X versions. So for example the top entry 10.1, shows that it deprecates less than 10 function from <font color=blue>10.0</font>. Then 10.3 deprecates a few symbols from <font color=blue>10.0</font> and  <font color="gold">10.2</font> and so on. Further interpretation of the data is left to the reader.
<p>
What is kind of interesting is to compare the raw numbers for the various SDKs. I checked 10.4.u.sdk, 10.5.sdk and 10.6.sdk. The horizontal axis has the version that <b>deprecates</b>, the vertical axis has the version that <b>suffers</b> deprecation:

<table>
<tr><td>
<table bgcolor="black" cellspacing=1>
<tr bgcolor="white"><th bgcolor="lightgray">10.4u.sdk</th><th bgcolor="lightgray">10.1</th><th bgcolor="lightgray">10.2</th><th bgcolor="lightgray">10.3</th><th bgcolor="lightgray">10.4</th><th bgcolor="lightgray">10.5</th><th bgcolor="lightgray">10.6</th></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.0</td><td>2</td><td>5</td><td>31</td><td>1119</td><td>82</td><td></td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.1</td><td></td><td>1</td><td>3</td><td>24</td><td></td><td></td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.2</td><td></td><td></td><td></td><td>20</td><td></td><td></td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.3</td><td></td><td></td><td></td><td>12</td><td></td><td></td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.4</td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.5</td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
</table>
</td><td>
<table bgcolor="black" cellspacing=1>
<tr bgcolor="white"><th bgcolor="lightgray">10.5.sdk</th><th bgcolor="lightgray">10.1</th><th bgcolor="lightgray">10.2</th><th bgcolor="lightgray">10.3</th><th bgcolor="lightgray">10.4</th><th bgcolor="lightgray">10.5</th><th bgcolor="lightgray">10.6</th></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.0</td><td>2</td><td>6</td><td>38</td><td>1107</td><td>682</td><td></td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.1</td><td></td><td></td><td>1</td><td>37</td><td>37</td><td></td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.2</td><td></td><td></td><td>9</td><td>22</td><td>19</td><td></td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.3</td><td></td><td></td><td></td><td>14</td><td>32</td><td></td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.4</td><td></td><td></td><td></td><td></td><td>7</td><td></td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.5</td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
</table>
</td></tr><tr><td>
<table bgcolor="black" cellspacing=1>
<tr bgcolor="#FEFEFE"><th bgcolor="lightgray">10.6.sdk</th><th bgcolor="lightgray">10.1</th><th bgcolor="lightgray">10.2</th><th bgcolor="lightgray">10.3</th><th bgcolor="lightgray">10.4</th><th bgcolor="lightgray">10.5</th><th bgcolor="lightgray">10.6</th></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.0</td><td>2</td><td>5</td><td>37</td><td>1082</td><td>640</td><td>97</td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.1</td><td></td><td></td><td>1</td><td>18</td><td>3</td><td>2</td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.2</td><td></td><td></td><td>9</td><td>21</td><td>12</td><td>3</td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.3</td><td></td><td></td><td></td><td>18</td><td>19</td><td>106</td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.4</td><td></td><td></td><td></td><td></td><td>7</td><td>13</td></tr>
<tr bgcolor="white"><th bgcolor="lightgray">10.5</td><td></td><td></td><td></td><td></td><td></td><td>8</td></tr>
</table>
</td></tr></table>

Some quick observations:
<ul>
<li>10.4u.sdk seems to be broken, as it has deprecations from 10.5 already
<li>since deprecations seem to disappear over time, this could mean, that either symbols were really removed, or that some symbols were wrongly deprecated
</li>
</ul>
<p><i>The methodology used here isn't perfect, because I am not checking all headers. But I hate scrapping the whole entry ;)</i><p>]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2010/01/deprecation_deprecation_deprec_1.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2010/01/deprecation_deprecation_deprec_1.html</guid>
        
        
         <pubDate>Wed, 06 Jan 2010 18:39:51 +0100</pubDate>
      </item>
      
      <item>
         <title>deprecation, deprecation, deprecation - Part 1</title>
         <description><![CDATA[The initial grievance for this posting is this warning:
<pre>foo.m:17: warning: 'NSLookupSymbolInImage' is deprecated 
(declared at /Developer/SDKs/MacOSX10.5.sdk/usr/include/mach-o/dyld.h:182)</pre>

When I look into the header I find this:
<pre>/*
 * The following dyld API's are deprecated as of Mac OS X 10.5.  They are either  
 * no longer necessary or are superceeded by dlopen and friends in <dlfcn.h>.
 * dlopen/dlsym/dlclose have been available since Mac OS X 10.3 and work with 
 * dylibs and bundles.  
 *
 *    NSAddImage                           -&gt; dlopen
 *    NSLookupSymbolInImage                -&gt; dlsym
 *    NSCreateObjectFileImageFromFile      -&gt; dlopen
 *    NSDestroyObjectFileImage             -&gt; dlclose
 *    NSLinkModule                         -&gt; not needed when dlopen used
 *    NSUnLinkModule                       -&gt; not needed when dlclose used
 *    NSLookupSymbolInModule               -&gt; dlsym
 *    _dyld_image_containing_address       -&gt; dladdr
 *    NSLinkEditError                      -&gt; dlerror
 *
 */
</pre>

and 
<pre>extern NSSymbol NSLookupSymbolInModule(NSModule module, const char* symbolName)
AVAILABLE_MAC_OS_X_VERSION_10_1_AND_LATER_BUT_DEPRECATED_IN_MAC_OS_X_VERSION_10_5;</pre>

So it appeared in 10.1 and it's plug gets pulled in 10.5. Short lived API, I would say.<p>
What bothers me, that one of the ideas behind <b><tt>Foundation</tt></b> is, that it should isolate me from the OS layer and abstract to a common denominator, that remains stable. Yet here, the <tt>Foundation</tt> functions are deprecated and I am suposed to use <tt>dyld</tt> directly, although <tt>Foundation</tt> still could (and probably does) do the work using <tt>dyld</tt>.
<p>
I don't get this.
]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2010/01/deprecation_deprecation_deprec.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2010/01/deprecation_deprecation_deprec.html</guid>
        
        
         <pubDate>Wed, 06 Jan 2010 18:11:47 +0100</pubDate>
      </item>
      
      <item>
         <title>Nat! vs. evil code elves: Final fishings before christmas</title>
         <description><![CDATA[Moving the framework to different start addresses:

<table><tr><th>Start Address</th><th>Result</th></tr>
<tr><td>0x34200000</td><td>no crash</td></tr>
<tr><td>0x00f50000</td><td>no crash</td></tr>
<tr><td>0x35210000</td><td>no crash</td></tr>
<tr><td>0x35200000</td><td><b>crash</b></td></tr>
<tr><td>0x75200000</td><td>no crash</td></tr>
</table>

So it it crashes with <b> 0x35200000</b>, well that I already did know.

<H3>Isolating the bug</H3>

I was able to isolate the bug eventually into a much much smaller project. Strangely enough, if I put the code into a <tt>.dylib</tt> then there is no problem, but if I use a <tt>.framework</tt> it crashes or fails. If I had tried a framework two days ago instead of a dylib, it would have saved me quite a bit of headache. Also I know now, that AppKit doesn't need to be present. A Foundation tool shows the same problem.
<p>
<b>The same code does not crash on another computer.</b>
<p>
Hilarious! After three days I know now much more about the bug, still, it seems to make no sense whatsoever. On a hunch I would say this has something to do with virtualization on the CPU in some way. It will be interesting to figure out, how to debug that.
<p>
As this is the last entry before christmas. Happy Christmas and New Years. More to come next year, if I don't die of old age in the mean time.

<H3>Final Note</h3>
I gave up on this. I assume this to have been a freak hardware problem.]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2009/12/nat_vs_evil_code_elves_final_f.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2009/12/nat_vs_evil_code_elves_final_f.html</guid>
        
        
         <pubDate>Wed, 23 Dec 2009 00:41:10 +0100</pubDate>
      </item>
      
      <item>
         <title>The plot thickens in the fight vs. the bug elves</title>
         <description><![CDATA[
The VPN is up again and I rearranged the code a little, and now I get a <b><tt>EXC_BAD_ACCESS</tt></b> in a completely different place: 
<pre>0x35204e9f  &lt;+0042&gt;  mov    (%eax),%eax
0x35204ea1  &lt;+0044&gt;  mov    %eax,0x4(%esp)
0x35204ea5  &lt;+0048&gt;  mov    %edx,(%esp)
0x35204ea8  &lt;+0051&gt;  call   0x35236170 &lt;dyld_stub_objc_msgSendSuper&gt;
0x35204ead  &lt;+0056&gt;  lea    0x2c2a1(%ebx),%eax
<b>0x35204eb3  &lt;+0062&gt;  mov    (%eax),%eax</b>
0x35204eb5  &lt;+0064&gt;  cmp    %eax,0x14(%ebp)
</pre>
<p>
compared to the original troublesome code
<p>
<pre>0x35204ea6  &lt;+0000>  push   %ebp
0x35204ea7  &lt;+0001>  mov    %esp,%ebp
0x35204ea9  &lt;+0003>  sub    $0x8,%esp
0x35204eac  &lt;+0006>  mov    0x8(%ebp),%eax
<b>0x35204eaf  &lt;+0009>  mov    0x8(%eax),%eax</b>
0x35204eb2  &lt;+0012>  leave  
0x35204eb3  &lt;+0013>  ret    
</pre>

Is it really a different place ? Actually in terms of adresses it's almost but not quite identical. Yes that still makes no sense whatsoever to me.
<p>
<h3>More fishing around</h3>

Dump of the shared library binary at the appropriate place. (I have put in four nops <b>0x90</b>, so I can easily identify it. (It stlll EXC_BAD_ACCESS crashes with the four nops)
<pre>00004ea0: 0489 1424 e8c7 1203 00<b>90 9090 90</b>8d 83a5  ...$............
00004eb0: c202 008b 0039 4514 7411 8d83 a1c2 0200  .....9E.t.......
00004ec0: 8b00 3945 140f 85ff 0000 0083 7d18 000f  ..9E........}...
</pre>
<i>It's interesting to note, that the offset into the linker file, is the same as the offset into a virtual memory page.</i> 
And here's the same memory seen by <tt>gdb</tt> post <tt>EXC_BAD_ACCESS</tt> crash:
<pre>0x35204ea0: 0x8904 0x2414 0xc7e8 0x0312 <b>0x90</b>00 <b>0x9090</b> 0x8d<b>90</b> 0xa583
0x35204eb0: 0x02c2 0x8b00 0x3900 0x1445 0x1174 0x838d 0xc2a1 0x0002
0x35204ec0: 0x008b 0x4539 0x0f14 0xff85 0x0000 0x8300 0x187d 0x0f00
</pre>
One  dump was made by <tt>emacs</tt> and the other with <tt>gdb</tt> and the endianness is different, but the instructions are identical.
<p>
<h3>One more nop and the crash is gone</h3>

So this is currently the code that gives the <tt> EXC_BAD_ACCESS</tt> at <b> 0x35204eb3</b> 
<pre>0x35204ea9  &lt;+0056&gt;  nop    
0x35204eaa  &lt;+0057&gt;  nop    
0x35204eab  &lt;+0058&gt;  nop    
0x35204eac  &lt;+0059&gt;  nop    
0x35204ead  &lt;+0060&gt;  lea    0x2c2a5(%ebx),%eax
0x35204eb3  &lt;+0066&gt;  mov    (%eax),%eax
0x35204eb5  &lt;+0068&gt;  cmp    %eax,0x14(%ebp)
</pre>

but if I put another <tt>nop</tt> there the crash is gone. Unfortunately as you can see, there is also a subtle shift of the code involved, which is due to the linker, which is out of my control (see: <a href="http://www.mulle-kybernetik.com/weblog/2009/12/some_more_fishing_around_the_e.html">previous entry</a>).

<pre>0x35204ea5  &lt;+0056&gt;  nop    
0x35204ea6  &lt;+0057&gt;  nop    
0x35204ea7  &lt;+0058&gt;  nop    
0x35204ea8  &lt;+0059&gt;  nop    
0x35204ea9  &lt;+0060&gt;  nop    
0x35204eaa  &lt;+0061&gt;  lea    0x2c2a9(%ebx),%eax
0x35204eb0  &lt;+0067&gt;  mov    (%eax),%eax
0x35204eb2  &lt;+0069&gt;  cmp    %eax,0x14(%ebp)
</pre>
]]></description>
         <link>http://www.mulle-kybernetik.com/weblog/2009/12/the_plot_thickens_in_the_fight.html</link>
         <guid>http://www.mulle-kybernetik.com/weblog/2009/12/the_plot_thickens_in_the_fight.html</guid>
        
        
         <pubDate>Tue, 22 Dec 2009 21:46:58 +0100</pubDate>
      </item>
      
   </channel>
</rss>
