<?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>Matt Galloway&#039;s iPhone Apps &#187; Uncategorized</title>
	<atom:link href="http://iphone.galloway.me.uk/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://iphone.galloway.me.uk</link>
	<description>iPhone Applications</description>
	<lastBuildDate>Thu, 02 Feb 2012 09:22:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>A look under ARC’s hood – Episode 3</title>
		<link>http://iphone.galloway.me.uk/2012/02/a-look-under-arcs-hood-%e2%80%93-episode-3/</link>
		<comments>http://iphone.galloway.me.uk/2012/02/a-look-under-arcs-hood-%e2%80%93-episode-3/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 10:46:38 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=509</guid>
		<description><![CDATA[This instalment of &#8220;A look under ARC&#8217;s hood&#8221; is all about the new @autoreleasepool directive. LLVM tells us that the semantics of autorelease pools has changed with LLVM 3.0 and in particular, I thought it might be interesting to see what ARC is doing when it comes to these. So consider the following method: This [...]]]></description>
			<content:encoded><![CDATA[<p>This instalment of &#8220;A look under ARC&#8217;s hood&#8221; is all about the new <code>@autoreleasepool</code> directive. <a href="http://clang.llvm.org/docs/AutomaticReferenceCounting.html#autoreleasepool">LLVM tells us</a> that the semantics of autorelease pools has changed with LLVM 3.0 and in particular, I thought it might be interesting to see what ARC is doing when it comes to these.</p>
<p><span id="more-509"></span></p>
<p>So consider the following method:</p>
<pre class="brush: objc; title: ; notranslate">
void foo() {
    @autoreleasepool {
        NSNumber *number = [NSNumber numberWithInt:0];
        NSLog(@&quot;number = %p&quot;, number);
    }
}
</pre>
<p>This is entirely contrived, of course, but it should let us see what&#8217;s going on. In non-ARC land we would assume here that <code>number</code> would be allocated inside <code>numberWithInt:</code> and returned autoreleased. So when the autorelease pool is next drained, it will be released. So let&#8217;s see if that&#8217;s what happened (as usual, this is ARMv7 instructions):</p>
<pre class="brush: cpp; highlight: [8,21,29]; title: ; notranslate">
        .globl  _foo
        .align  2
        .code   16
        .thumb_func     _foo
_foo:
        push    {r4, r7, lr}
        add     r7, sp, #4
        blx     _objc_autoreleasePoolPush
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        movs    r2, #0
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        mov     r4, r0
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
LPC0_0:
        add     r1, pc
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
LPC0_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        mov     r1, r0
        movw    r0, :lower16:(L__unnamed_cfstring_-(LPC0_2+4))
        movt    r0, :upper16:(L__unnamed_cfstring_-(LPC0_2+4))
LPC0_2:
        add     r0, pc
        blx     _NSLog
        mov     r0, r4
        blx     _objc_autoreleasePoolPop
        pop     {r4, r7, pc}
</pre>
<p>Well, yes. That&#8217;s exactly what&#8217;s happening. We can see the call to push an autorelease pool then a call to <code>numberWithInt:</code> then a call to pop an autorelease pool. Exactly what we&#8217;d expect. Now let&#8217;s look at the exact same code compiled under ARC:</p>
<pre class="brush: cpp; highlight: [25,34]; title: ; notranslate">
        .globl  _foo
        .align  2
        .code   16
        .thumb_func     _foo
_foo:
        push    {r4, r5, r7, lr}
        add     r7, sp, #8
        blx     _objc_autoreleasePoolPush
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        movs    r2, #0
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC0_0+4))
        mov     r4, r0
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
LPC0_0:
        add     r1, pc
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC0_1+4))
LPC0_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        @ InlineAsm Start
        mov     r7, r7          @ marker for objc_retainAutoreleaseReturnValue
        @ InlineAsm End
        blx     _objc_retainAutoreleasedReturnValue
        mov     r5, r0
        movw    r0, :lower16:(L__unnamed_cfstring_-(LPC0_2+4))
        movt    r0, :upper16:(L__unnamed_cfstring_-(LPC0_2+4))
        mov     r1, r5
LPC0_2:
        add     r0, pc
        blx     _NSLog
        mov     r0, r5
        blx     _objc_release
        mov     r0, r4
        blx     _objc_autoreleasePoolPop
        pop     {r4, r5, r7, pc}
</pre>
<p>Notice the calls to <code>objc_retainAutoreleasedReturnValue</code> and <code>objc_release</code>. What&#8217;s happening there is that ARC has determined for us that it doesn&#8217;t really need to worry about the autorelease pool that&#8217;s in place, because it can simply tell the autorelease to not happen (with the call to <code>objc_retainAutoreleasedReturnValue</code>) and then release the object later itself. This is desirable as it means the autorelease logic doesn&#8217;t have to happen.</p>
<p>Note that the autorelease pool is still required to be pushed and popped because ARC can&#8217;t know what&#8217;s going on in the calls to <code>numberWithInt:</code> and <code>NSLog</code> to know if objects will be put into the pool there. If it did know that they didn&#8217;t autorelease anything then it could actually get rid of the push and pop. Perhaps that kind of logic will come in future versions although I&#8217;m not quite sure how the semantics of that would work though.</p>
<p>Now let&#8217;s consider another example which is where we want to use <code>number</code> outside of the scope of the autorelease pool block. This should show us why ARC is a wonder to work with. Consider the following code:</p>
<pre class="brush: objc; title: ; notranslate">
void bar() {
    NSNumber *number;
    @autoreleasepool {
        number = [NSNumber numberWithInt:0];
        NSLog(@&quot;number = %p&quot;, number);
    }
    NSLog(@&quot;number = %p&quot;, number);
}
</pre>
<p>You might be (correctly) thinking that this is going to cause problems even though it looks perfectly innocuous. It&#8217;s a problem because <code>number</code> will be allocated inside the autorelease pool block, will be deallocated when the autorelease pool pops but is then used after it&#8217;s been deallocated. Uh oh! Let&#8217;s see if we&#8217;re right by compiling it without ARC enabled:</p>
<pre class="brush: cpp; title: ; notranslate">
        .globl  _bar
        .align  2
        .code   16
        .thumb_func     _bar
_bar:
        push    {r4, r5, r6, r7, lr}
        add     r7, sp, #12
        blx     _objc_autoreleasePoolPush
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        movs    r2, #0
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        mov     r4, r0
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
LPC1_0:
        add     r1, pc
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
LPC1_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        movw    r6, :lower16:(L__unnamed_cfstring_-(LPC1_2+4))
        movt    r6, :upper16:(L__unnamed_cfstring_-(LPC1_2+4))
LPC1_2:
        add     r6, pc
        mov     r5, r0
        mov     r1, r5
        mov     r0, r6
        blx     _NSLog
        mov     r0, r4
        blx     _objc_autoreleasePoolPop
        mov     r0, r6
        mov     r1, r5
        blx     _NSLog
        pop     {r4, r5, r6, r7, pc}
</pre>
<p>Obviously no calls to retain, release or autorelease as we&#8217;d expect since we haven&#8217;t made any explicitly and we&#8217;re not using ARC. We can see here that it&#8217;s been compiled exactly as we&#8217;d expect from our reasoning before. So let&#8217;s see what it looks like when ARC gives us a helping hand:</p>
<pre class="brush: cpp; highlight: [25,40]; title: ; notranslate">
        .globl  _bar
        .align  2
        .code   16
        .thumb_func     _bar
_bar:
        push    {r4, r5, r6, r7, lr}
        add     r7, sp, #12
        blx     _objc_autoreleasePoolPush
        movw    r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        movs    r2, #0
        movt    r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC1_0+4))
        mov     r4, r0
        movw    r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
LPC1_0:
        add     r1, pc
        movt    r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC1_1+4))
LPC1_1:
        add     r0, pc
        ldr     r1, [r1]
        ldr     r0, [r0]
        blx     _objc_msgSend
        @ InlineAsm Start
        mov     r7, r7          @ marker for objc_retainAutoreleaseReturnValue
        @ InlineAsm End
        blx     _objc_retainAutoreleasedReturnValue
        movw    r6, :lower16:(L__unnamed_cfstring_-(LPC1_2+4))
        movt    r6, :upper16:(L__unnamed_cfstring_-(LPC1_2+4))
LPC1_2:
        add     r6, pc
        mov     r5, r0
        mov     r1, r5
        mov     r0, r6
        blx     _NSLog
        mov     r0, r4
        blx     _objc_autoreleasePoolPop
        mov     r0, r6
        mov     r1, r5
        blx     _NSLog
        mov     r0, r5
        blx     _objc_release
        pop     {r4, r5, r6, r7, pc}
</pre>
<p>Round of applause for ARC please! Notice that it&#8217;s realised we&#8217;re using <code>number</code> outside of the scope of the autorelease pool block so it&#8217;s retained the return value from <code>numberWithInt:</code> just as it did before, but this time it&#8217;s placed the release at the end of the <code>bar</code> function rather than before the autorelease pool is popped. That will have saved us a crash in some code that we might have thought was correct but actually had a subtle memory management bug.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/02/a-look-under-arcs-hood-%e2%80%93-episode-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UIImageOrientation / EXIF orientation sample images</title>
		<link>http://iphone.galloway.me.uk/2012/01/uiimageorientation-exif-orientation-sample-images/</link>
		<comments>http://iphone.galloway.me.uk/2012/01/uiimageorientation-exif-orientation-sample-images/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 18:18:22 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=491</guid>
		<description><![CDATA[Whilst I was doing some work with images recently I was in desperate need for some sample images which were tagged with the EXIF orientation flag for each of the 8 orientations which are supported by UIImage in its UIImageOrientation metadata. I couldn&#8217;t find any already out there, so I made my own. And now [...]]]></description>
			<content:encoded><![CDATA[<p>Whilst I was doing some work with images recently I was in desperate need for some sample images which were tagged with the EXIF orientation flag for each of the 8 orientations which are supported by UIImage in its <code>UIImageOrientation</code> metadata. I couldn&#8217;t find any already out there, so I made my own. And now I&#8217;m posting them here for anyone who might also find this useful. I now have these saved on my iPhone and use them in apps as test images.</p>
<p><a href='http://iphone.galloway.me.uk/wp-content/uploads/2012/01/EXIF_Orientation_Samples.zip'>Download All</a></p>
<p><span id="more-491"></span></p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/up.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/up-225x300.jpg" alt="" title="up" width="225" height="300" class="aligncenter size-medium wp-image-499" style="background-color: #333; padding: 4px; margin: 10px" /></a> <a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/up-mirrored.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/up-mirrored-225x300.jpg" alt="" title="up-mirrored" width="225" height="300" class="aligncenter size-medium wp-image-498" style="background-color: #333; padding: 4px; margin: 10px" /></a></p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/down.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/down-225x300.jpg" alt="" title="down" width="225" height="300" class="aligncenter size-medium wp-image-493" style="background-color: #333; padding: 4px; margin: 10px" /></a> <a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/down-mirrored.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/down-mirrored-225x300.jpg" alt="" title="down-mirrored" width="225" height="300" class="aligncenter size-medium wp-image-492" style="background-color: #333; padding: 4px; margin: 10px" /></a></p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/left.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/left-300x225.jpg" alt="" title="left" width="300" height="225" class="aligncenter size-medium wp-image-495" style="background-color: #333; padding: 4px; margin: 10px" /></a> <a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/left-mirrored.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/left-mirrored-300x225.jpg" alt="" title="left-mirrored" width="300" height="225" class="aligncenter size-medium wp-image-494" style="background-color: #333; padding: 4px; margin: 10px" /></a></p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/right.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/right-300x225.jpg" alt="" title="right" width="300" height="225" class="aligncenter size-medium wp-image-497" style="background-color: #333; padding: 4px; margin: 10px" /></a> <a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/right-mirrored.jpg" rel="lightbox[491]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/right-mirrored-300x225.jpg" alt="" title="right-mirrored" width="300" height="225" class="aligncenter size-medium wp-image-496" style="background-color: #333; padding: 4px; margin: 10px" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/01/uiimageorientation-exif-orientation-sample-images/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to visualise affine transforms when drawing with Quartz 2D.</title>
		<link>http://iphone.galloway.me.uk/2012/01/how-to-visualise-affine-transforms-when-drawing-with-quartz-2d/</link>
		<comments>http://iphone.galloway.me.uk/2012/01/how-to-visualise-affine-transforms-when-drawing-with-quartz-2d/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 18:09:00 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=484</guid>
		<description><![CDATA[I had been struggling with some code that I had to rotate an image whilst drawing it so I decided to sit down and work out a nice way to visualise it as I hadn&#8217;t seen anything out there really that explained it very well. This is what I came up with&#8230; In order to [...]]]></description>
			<content:encoded><![CDATA[<p>I had been <a href="http://stackoverflow.com/questions/9062363/cgcontext-drawing-rotated-around-arbitrary-point">struggling with some code</a> that I had to rotate an image whilst drawing it so I decided to sit down and work out a nice way to visualise it as I hadn&#8217;t seen anything out there really that explained it very well. This is what I came up with&#8230;</p>
<p><span id="more-484"></span></p>
<p>In order to visualise it I decided to create a card with an &#8216;F&#8217; on the front and then trace through the &#8216;F&#8217; onto the back to that effectively it is mirrored. The purpose of the mirror is to enable me to visualise the case when dealing with images that are mirrored (i.e. <code>UIImageOrientationUpMirrored</code>, <code>UIImageOrientationDownMirrored</code>, etc). Here is an example of my card:</p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Sample-Card.png" rel="lightbox[484]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Sample-Card.png" alt="" title="Sample-Card" width="400" height="354" class="aligncenter size-full wp-image-487" /></a></p>
<p>Now we can get to work using that to work out how we might want to translate &#038; rotate a context whilst drawing into. Consider first of all an image that is in the <code>UIImageOrientationDown</code> orientation. So hold your card with the &#8216;F&#8217; on it with the front facing you and rotate it through 180 degrees. That is the orientation we want to determine the correct transformation matrix for in this case. Now since Quartz draws with the origin in the bottom right, you need to imagine a coordinate system where the y-axis is going up from the bottom to the top of your card and the x-axis is going across from the left to the right. I like to imagine little arrows going up &#038; left from the bottom left corner of my card.</p>
<p>So to get this to draw into our canvas correctly it&#8217;s pretty clear that we need to rotate by 180 degrees. A rotation will rotate about the origin of our coordinate system so if we purely rotate by 180 degrees then the image will be drawn the right way up but will lie down and left from the origin. To see why this is, hold your card with your fingers at the bottom left. Now imagine the canvas going up and right from here, the size of the card. Now push the card round from the top right corner by 180 degrees, pivoting about the bottom left. You can see that nothing would be drawn into the canvas! So what we need to do is translate up by the height and right by the width. That then brings the image into the right place in the canvas.</p>
<p>I like to draw out this like so on paper:</p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Down-Example.png" rel="lightbox[484]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Down-Example.png" alt="" title="Down-Example" width="640" height="452" class="aligncenter size-full wp-image-485" /></a></p>
<p>I think this helps explain it because if at all times you keep your little coordinate arrows on your card lined up with the ones in the diagram, you can visualise what&#8217;s going on.</p>
<p>Now let&#8217;s consider a much more complicated example, the case of <code>UIImageOrientationLeftMirrored</code>. Start with your card on its front and on its side with the long side of the &#8216;F&#8217; at the bottom. Then flip it over. This is now in the desired orientation.</p>
<p>It&#8217;s hard to work out from here what needs to be done but if you follow through the diagram below and at all times keeping your little coordinate arrows on your card aligned with the arrows on the diagram then you should be able to convince yourself that it&#8217;s right.</p>
<p><a href="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Left-Mirrored-Example.png" rel="lightbox[484]"><img src="http://iphone.galloway.me.uk/wp-content/uploads/2012/01/Left-Mirrored-Example.png" alt="" title="Left-Mirrored-Example" width="640" height="452" class="aligncenter size-full wp-image-486" /></a></p>
<p>So to follow this you would hold your card with the &#8216;F&#8217; in the position described above with its bottom left aligned with the bottom left of the canvas &#8217;1&#8242; on the diagram. Then to get to position 2 you would move the card horizontally across the width of the canvas where your little arrows should line up with the arrows in the diagram on canvas &#8217;2&#8242;. To get to position 3 you would rotate 90 degrees counter-clockwise (i.e. positive rotation &#8211; which is rotating from the positive x-axis towards the positive y-axis). Then to get to position 4 you need to move the card vertically, which is a translation in positive x if you look at it carefully. Then finally to get to position 5 you need to flip the card along the y-axis which brings the image into the canvas fully and you&#8217;ll notice the &#8216;F&#8217; is the right way up!</p>
<p>Note that if we want to then draw the image we would need to define the rectangle to draw into with a width equal to the canvas height and a height equal to the canvas width because our x axis is now vertical and our y axis is horizontal. Alternatively think of it that the height of the image is the width of the canvas and the width of the image is the height of the canvas.</p>
<p>I used this method to work out how to draw a <code>UIImage</code> rotated correctly to its upright position. Here is that code:</p>
<pre class="brush: objc; title: ; notranslate">
UIImage *image = &lt;THE_IMAGE&gt;;
CGSize imageSize = image.size;

CGRect imageRect = CGRectIntegral(CGRectMake(0.0f, 0.0f, imageSize.width, imageSize.height));
CGRect imageTransposedRect = CGRectMake(0.0f, 0.0f, imageSize.height, imageSize.width);

CGAffineTransform transform = CGAffineTransformIdentity;
CGRect drawRect = imageRect;

switch (image.imageOrientation) {
    case UIImageOrientationUp:
        break;
    case UIImageOrientationUpMirrored:
        transform = CGAffineTransformTranslate(transform, rect.size.width, 0.0f);
        transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
        break;
    case UIImageOrientationDown:
        transform = CGAffineTransformTranslate(transform, rect.size.width, rect.size.height);
        transform = CGAffineTransformRotate(transform, M_PI);
        break;
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, 0.0f, rect.size.height);
        transform = CGAffineTransformScale(transform, 1.0f, -1.0f);
        break;
    case UIImageOrientationLeft:
        transform = CGAffineTransformTranslate(transform, rect.size.width, 0.0f);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        drawRect = imageTransposedRect;
        break;
    case UIImageOrientationLeftMirrored:
        transform = CGAffineTransformTranslate(transform, rect.size.width, 0.0f);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        transform = CGAffineTransformTranslate(transform, rect.size.height, 0.0f);
        transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
        drawRect = imageTransposedRect;
        break;
    case UIImageOrientationRight:
        transform = CGAffineTransformTranslate(transform, 0.0f, rect.size.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        drawRect = imageTransposedRect;
        break;
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        drawRect = imageTransposedRect;
        break;
}

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                            drawRect.size.width,
                                            drawRect.size.height,
                                            8,
                                            0,
                                            colorSpace,
                                            kCGImageAlphaPremultipliedLast);

CGContextConcatCTM(bitmap, transform);
CGContextDrawImage(bitmap, drawRect, image);

CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

CGContextRelease(bitmap);
CGImageRelease(newImageRef);
CGColorSpaceRelease(colorSpace);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2012/01/how-to-visualise-affine-transforms-when-drawing-with-quartz-2d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GiffGaff: Great for iPad!</title>
		<link>http://iphone.galloway.me.uk/2011/11/giffgaff-great-for-ipad/</link>
		<comments>http://iphone.galloway.me.uk/2011/11/giffgaff-great-for-ipad/#comments</comments>
		<pubDate>Sat, 12 Nov 2011 14:32:51 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=432</guid>
		<description><![CDATA[I&#8217;ve heard about GiffGaff from a lot of people but it was only when I realised how good it would be for my iPad that I decided to give it a go. So I got a SIM, topped up £10 and now I can spend just 20p each day and get 20MB of data! It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve heard about <a href="http://giffgaff.com/orders/affiliate/mattjgalloway_ipad">GiffGaff</a> from a lot of people but it was only when I realised how good it would be for my iPad that I decided to give it a go. So I got a SIM, topped up £10 and now I can spend just 20p each day and get 20MB of data! It&#8217;s great for my iPad because I can use it whilst out and about and just pay for each day that I want mobile data, which is quite rare for me really.</p>
<p>So if you&#8217;ve got an iPad and want cheap data for it then go <a href="http://giffgaff.com/orders/affiliate/mattjgalloway_ipad">grab a GiffGaff SIM</a> and away you go!</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2011/11/giffgaff-great-for-ipad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[UPDATED - Not a bug!] Another interesting compiler bug (in Apple&#8217;s LLVM this time)</title>
		<link>http://iphone.galloway.me.uk/2011/06/another-interesting-compiler-bug-in-apples-llvm-this-time/</link>
		<comments>http://iphone.galloway.me.uk/2011/06/another-interesting-compiler-bug-in-apples-llvm-this-time/#comments</comments>
		<pubDate>Tue, 21 Jun 2011 13:57:09 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=419</guid>
		<description><![CDATA[I came across another interesting compiler bug today. I&#8217;m not going to go into too much detail about it, but the problem code is this: To break that down a bit, the function is doing this: Initialise a couple of variables and set them to 0. Then perform some inline assembly using the variables. This [...]]]></description>
			<content:encoded><![CDATA[<p>I came across another interesting compiler bug today. I&#8217;m not going to go into too much detail about it, but the problem code is this:</p>
<pre class="brush: cpp; title: ; notranslate">
void a() {
    int a = 0;
    int b = 0;
    __asm__(&quot;\n&quot;
            &quot;\tmov %0, 0\n&quot;
            &quot;\tldr %0, %1\n&quot;
            &quot;\tmov %1, 0\n&quot;
            : &quot;=r&quot;(a), &quot;+m&quot;(b)
           );
}
</pre>
<p>To break that down a bit, the function is doing this:</p>
<ol>
<li>
<pre class="brush: cpp; title: ; notranslate">
    int a = 0;
    int b = 0;
</pre>
<p>Initialise a couple of variables and set them to 0.
	</li>
<li>
<pre class="brush: cpp; title: ; notranslate">
    __asm__(&quot;\n&quot;
            &quot;\tmov %0, 0\n&quot;
            &quot;\tldr %0, %1\n&quot;
            &quot;\tmov %1, 0\n&quot;
            : &quot;=r&quot;(a), &quot;+m&quot;(b)
           );
</pre>
<p>Then perform some inline assembly using the variables. This just puts 0 into the register that will hold our &#8216;a&#8217; variable, then loads the value of &#8216;b&#8217; into &#8216;a&#8217;, then sets &#8216;b&#8217; to 0.
	</li>
</ol>
<p>This is of course a contrived example, but it illustrates the bug. The output assembly from LLVM-GCC or clang is (for ARM architecture):</p>
<pre class="brush: plain; title: ; notranslate">
        .globl  _a
        .align  2
        .code   16
        .thumb_func     _a
_a:
        sub     sp, #8
        movs    r0, #0
        movt    r0, #0
        str     r0, [sp, #4]
        str     r0, [sp]
        mov     r0, sp
        @ InlineAsm Start

        mov r0, 0
        ldr r0, [r0]
        mov [r0], 0

        @ InlineAsm End
        str     r0, [sp, #4]
        add     sp, #8
        bx      lr
</pre>
<p>The interesting bit is the inline assembly. You&#8217;ll notice that it&#8217;s doing something very stupid. It&#8217;s choosing the same register for both operands (it&#8217;s choosing r0). This is completely wrong, and will lead to a runtime crash in this case due to the dereference of 0.</p>
<p>I did a bit of hunting and it appears to be a problem in generating the LLVM bytecode as the problem manifests itself before the LLVM bytecode is compiled down into instructions, like so:</p>
<pre class="brush: plain; title: ; notranslate">
define void @a() nounwind ssp {
  %a = alloca i32, align 4
  %b = alloca i32, align 4
  store i32 0, i32* %a, align 4
  store i32 0, i32* %b, align 4
  %1 = call i32 asm &quot;&#92;&#48;A&#92;&#48;9mov $0, 0&#92;&#48;A&#92;&#48;9ldr $0, $1&#92;&#48;A&#92;&#48;9mov $1, 0&#92;&#48;A&quot;, &quot;=r,=*m,*m&quot;(i32* %b, i32* %b) nounwind, !srcloc !0
  store i32 %1, i32* %a, align 4
  ret void
}
</pre>
<p>You can see here that the &#8216;%a&#8217; (i.e. variable &#8216;a&#8217;) is never referenced, only &#8216;%b&#8217; (i.e. variable &#8216;a&#8217;). This is not what we&#8217;d expect at all given we&#8217;re referencing both variables in the code.</p>
<p>I found this quite interesting <img src='http://iphone.galloway.me.uk/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p><strong>[Update]</strong></p>
<p>I&#8217;ve actually found out that this isn&#8217;t a bug! That&#8217;s good news, right? It&#8217;s quite a subtle thing, but the heart of the problem can be explained after understanding the <a href="http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Modifiers.html#Modifiers">modifiers to operands</a> on inline assembly. The problem is that we&#8217;re not specifying that &#8216;a&#8217; is <em>clobbered early</em>. In the assembly, we&#8217;re writing to it before reading we&#8217;ve finished using all input operands (only &#8216;b&#8217; in this case) so we&#8217;re meant to mark it like that. It&#8217;s just luck that GCC does the right thing &#8211; Apple&#8217;s LLVM is doing the right thing and using less registers!</p>
<p>So the correct code is this:</p>
<pre class="brush: cpp; title: ; notranslate">
void a() {
    int a = 0;
    int b = 0;
    __asm__(&quot;\n&quot;
            &quot;\tmov %0, 0\n&quot;
            &quot;\tldr %0, %1\n&quot;
            &quot;\tmov %1, 0\n&quot;
            : &quot;=&amp;r&quot;(a), &quot;+m&quot;(b)
           );
}
</pre>
<p>Which results in the following assembly:</p>
<pre class="brush: plain; title: ; notranslate">
        .globl  _a
        .align  2
        .code   16
        .thumb_func     _a
_a:
        sub     sp, #8
        movs    r0, #0
        movt    r0, #0
        str     r0, [sp, #4]
        str     r0, [sp]
        mov     r0, sp
        @ InlineAsm Start

        mov r1, 0
        ldr r1, [r0]
        mov [r0], 0

        @ InlineAsm End
        str     r1, [sp, #4]
        add     sp, #8
        bx      lr
</pre>
<p>And the following LLVM:</p>
<pre class="brush: plain; title: ; notranslate">
define void @a() nounwind ssp {
  %a = alloca i32, align 4
  %b = alloca i32, align 4
  store i32 0, i32* %a, align 4
  store i32 0, i32* %b, align 4
  %1 = call i32 asm &quot;&#92;&#48;A&#92;&#48;9mov $0, 0&#92;&#48;A&#92;&#48;9ldr $0, $1&#92;&#48;A&#92;&#48;9mov $1, 0&#92;&#48;A&quot;, &quot;=&amp;r,=*m,*m&quot;(i32* %b, i32* %b) nounwind, !srcloc !0
  store i32 %1, i32* %a, align 4
  ret void
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2011/06/another-interesting-compiler-bug-in-apples-llvm-this-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Assembly &#8211; beware local label names with &#8220;-dead_strip&#8221; option!</title>
		<link>http://iphone.galloway.me.uk/2011/03/assembly-beware-local-label-names-with-dead_strip-option/</link>
		<comments>http://iphone.galloway.me.uk/2011/03/assembly-beware-local-label-names-with-dead_strip-option/#comments</comments>
		<pubDate>Mon, 28 Mar 2011 13:08:47 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=393</guid>
		<description><![CDATA[I came across a very strange bug whilst developing an iOS application whereby the application would seg fault and whilst stepping through the code I found it was going all over the place. This lead me to run the application through otool, and I discovered that half the code for a function was missing! Here [...]]]></description>
			<content:encoded><![CDATA[<p>I came across a very strange bug whilst developing an iOS application whereby the application would seg fault and whilst stepping through the code I found it was going all over the place. This lead me to run the application through otool, and I discovered that half the code for a function was missing!</p>
<p>Here is an example of what happened&#8230;</p>
<p>Consider the following C file. It&#8217;s just a very simple function that has some inline assembly (to count down from 10 to 0) and a very simple function that does absolutely nothing.</p>
<pre class="brush: cpp; title: ; notranslate">
void func() {
    int tmp;
    __asm__ __volatile__ (
        &quot;\tmov %0, #10\n&quot;

        &quot;.loop:\n&quot;
        &quot;\tsubs %0, %0, #1\n&quot;
        &quot;\tbne .loop\n&quot;

        : &quot;=r&quot; (tmp)
        : &quot;r&quot; (tmp)
    );
}

void funcB() {
}
</pre>
<p>Let&#8217;s see what happens when we compile it for iOS&#8230;</p>
<pre class="brush: plain; title: ; notranslate">
        .section __TEXT,__text,regular
        .section __TEXT,__textcoal_nt,coalesced
        .section __TEXT,__const_coal,coalesced
        .section __TEXT,__picsymbolstub4,symbol_stubs,none,16
        .text
        .align 2
        .globl _func
_func:
        @ args = 0, pretend = 0, frame = 4
        @ frame_needed = 1, uses_anonymous_args = 0
        stmfd   sp!, {r7, lr}
        add     r7, sp, #0
        sub     sp, sp, #4
        ldr     r3, [sp]
                mov r3, #10
.loop:
        subs r3, r3, #1
        bne .loop

        str     r3, [sp]
        sub     sp, r7, #0
        ldmfd   sp!, {r7, pc}
        .align 2
        .globl _funcB
_funcB:
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 1, uses_anonymous_args = 0
        stmfd   sp!, {r7, lr}
        add     r7, sp, #0
        ldmfd   sp!, {r7, pc}
        .subsections_via_symbols
</pre>
<p>That all looks fairly normal and we could quite happily believe that was going to work just fine. So, let&#8217;s use this function in a test application. The code below is just a very simple application that calls the &#8216;func()&#8217; function and then returns.</p>
<pre class="brush: cpp; title: ; notranslate">
void func();
int main() {
    func();
    return 0;
}
</pre>
<p>So now let&#8217;s see what happens when we link this with the same options that you would have on by default in an iOS application (hint: -dead_strip is active).</p>
<p>Output from &#8216;otool -vV -t&#8217; on the linked application:</p>
<pre class="brush: plain; title: ; notranslate">
_main:
00002fa0        e92d4080        push    {r7, lr}
00002fa4        e28d7000        add     r7, sp, #0      @ 0x0
00002fa8        eb000002        bl      _func
00002fac        e3a03000        mov     r3, #0  @ 0x0
00002fb0        e1a00003        mov     r0, r3
00002fb4        e8bd8080        pop     {r7, pc}
_func:
00002fb8        e92d4080        push    {r7, lr}
00002fbc        e28d7000        add     r7, sp, #0      @ 0x0
00002fc0        e24dd004        sub     sp, sp, #4      @ 0x4
00002fc4        e59d3000        ldr     r3, [sp]
00002fc8        e3a0300a        mov     r3, #10 @ 0xa
</pre>
<p>What?! Where&#8217;s the rest of the &#8216;func()&#8217; code?! Not only is it missing, but it would appear that &#8216;func()&#8217; just simply stops without ever returning?! That looks very suspicious&#8230; So, let&#8217;s compile it without the &#8216;-dead_strip&#8217; option:</p>
<p>Output from &#8216;otool -vV -t&#8217; on the linked application:</p>
<pre class="brush: plain; title: ; notranslate">
_main:
00002f80        e92d4080        push    {r7, lr}
00002f84        e28d7000        add     r7, sp, #0      @ 0x0
00002f88        eb000002        bl      _func
00002f8c        e3a03000        mov     r3, #0  @ 0x0
00002f90        e1a00003        mov     r0, r3
00002f94        e8bd8080        pop     {r7, pc}
_func:
00002f98        e92d4080        push    {r7, lr}
00002f9c        e28d7000        add     r7, sp, #0      @ 0x0
00002fa0        e24dd004        sub     sp, sp, #4      @ 0x4
00002fa4        e59d3000        ldr     r3, [sp]
00002fa8        e3a0300a        mov     r3, #10 @ 0xa
.loop:
00002fac        e2533001        subs    r3, r3, #1      @ 0x1
00002fb0        1afffffd        bne     .loop
00002fb4        e58d3000        str     r3, [sp]
00002fb8        e247d000        sub     sp, r7, #0      @ 0x0
00002fbc        e8bd8080        pop     {r7, pc}
_funcB:
00002fc0        e92d4080        push    {r7, lr}
00002fc4        e28d7000        add     r7, sp, #0      @ 0x0
00002fc8        e8bd8080        pop     {r7, pc}
</pre>
<p>Ah, that&#8217;s better! The loop is back and so is the return. Also, &#8216;funcB()&#8217; is still in there. So, what has happened you may ask. Well, -dead_strip is designed to remove symbols from a binary that are not required. So we&#8217;d expect &#8216;funcB&#8217; to be removed, but not .loop as it&#8217;s part of &#8216;func&#8217;. However, if we look closer, what has happened is that &#8216;.loop&#8217; has become a top level symbol rather than a symbol local to the &#8216;func&#8217; symbol. So -dead_strip assumed that it was a symbol not used anywhere (as it&#8217;s only accessed from within .loop itself) and so it removed it, resulting in a completely mangled application binary.</p>
<p>To stop this happening you must always prefix your local symbols with &#8216;L&#8217; (as per the GCC documentation!). But I think this is an excellent example of what can go wrong if you just do 1 tiny thing wrong with inline assembly.</p>
<p>As a side note, I decided to try compiling/assembling/linking all of the above for Android as well. Interestingly the results were different. First I&#8217;ll show the outputs of the various stages and then explain the results.</p>
<p>The assembly generated for Android:</p>
<pre class="brush: plain; title: ; notranslate">
        .arch armv6
        .fpu softvfp
        .eabi_attribute 20, 1
        .eabi_attribute 21, 1
        .eabi_attribute 23, 3
        .eabi_attribute 24, 1
        .eabi_attribute 25, 1
        .eabi_attribute 26, 2
        .eabi_attribute 30, 6
        .eabi_attribute 18, 4
        .file   &quot;test.c&quot;
        .text
        .align  2
        .global func
        .type   func, %function
func:
        @ args = 0, pretend = 0, frame = 8
        @ frame_needed = 1, uses_anonymous_args = 0
        @ link register save eliminated.
        str     fp, [sp, #-4]!
        add     fp, sp, #0
        sub     sp, sp, #12
        ldr     r3, [fp, #-8]
#APP
@ 3 &quot;test.c&quot; 1
                mov r3, #10
.loop:
        subs r3, r3, #1
        bne .loop

@ 0 &quot;&quot; 2
        str     r3, [fp, #-8]
        add     sp, fp, #0
        ldmfd   sp!, {fp}
        bx      lr
        .size   func, .-func
        .align  2
        .global funcB
        .type   funcB, %function
funcB:
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 1, uses_anonymous_args = 0
        @ link register save eliminated.
        str     fp, [sp, #-4]!
        add     fp, sp, #0
        add     sp, fp, #0
        ldmfd   sp!, {fp}
        bx      lr
        .size   funcB, .-funcB
        .ident  &quot;GCC: (GNU) 4.4.3&quot;
        .section        .note.GNU-stack,&quot;&quot;,%progbits
</pre>
<p>Disassembly of linked app for Android (without stripping):</p>
<pre class="brush: plain; title: ; notranslate">
000082e0 &lt;main&gt;:
    82e0:       e92d4800        push    {fp, lr}
    82e4:       e28db004        add     fp, sp, #4      ; 0x4
    82e8:       eb000002        bl      82f8 &lt;func&gt;
    82ec:       e3a03000        mov     r3, #0  ; 0x0
    82f0:       e1a00003        mov     r0, r3
    82f4:       e8bd8800        pop     {fp, pc}

000082f8 &lt;func&gt;:
    82f8:       e52db004        push    {fp}            ; (str fp, [sp, #-4]!)
    82fc:       e28db000        add     fp, sp, #0      ; 0x0
    8300:       e24dd00c        sub     sp, sp, #12     ; 0xc
    8304:       e51b3008        ldr     r3, [fp, #-8]
    8308:       e3a0300a        mov     r3, #10 ; 0xa

0000830c &lt;.loop&gt;:
    830c:       e2533001        subs    r3, r3, #1      ; 0x1
    8310:       1afffffd        bne     830c &lt;.loop&gt;
    8314:       e50b3008        str     r3, [fp, #-8]
    8318:       e28bd000        add     sp, fp, #0      ; 0x0
    831c:       e8bd0800        pop     {fp}
    8320:       e12fff1e        bx      lr

00008324 &lt;funcB&gt;:
    8324:       e52db004        push    {fp}            ; (str fp, [sp, #-4]!)
    8328:       e28db000        add     fp, sp, #0      ; 0x0
    832c:       e28bd000        add     sp, fp, #0      ; 0x0
    8330:       e8bd0800        pop     {fp}
    8334:       e12fff1e        bx      lr
</pre>
<p>Disassembly of linked app for Android (after using strip command):</p>
<pre class="brush: plain; title: ; notranslate">
000082e0 &lt;main&gt;:
    82e0:       e92d4800        push    {fp, lr}
    82e4:       e28db004        add     fp, sp, #4      ; 0x4
    82e8:       eb000002        bl      82f8 &lt;func&gt;
    82ec:       e3a03000        mov     r3, #0  ; 0x0
    82f0:       e1a00003        mov     r0, r3
    82f4:       e8bd8800        pop     {fp, pc}

000082f8 &lt;func&gt;:
    82f8:       e52db004        push    {fp}            ; (str fp, [sp, #-4]!)
    82fc:       e28db000        add     fp, sp, #0      ; 0x0
    8300:       e24dd00c        sub     sp, sp, #12     ; 0xc
    8304:       e51b3008        ldr     r3, [fp, #-8]
    8308:       e3a0300a        mov     r3, #10 ; 0xa
    830c:       e2533001        subs    r3, r3, #1      ; 0x1
    8310:       1afffffd        bne     830c &lt;func+0x14&gt;
    8314:       e50b3008        str     r3, [fp, #-8]
    8318:       e28bd000        add     sp, fp, #0      ; 0x0
    831c:       e8bd0800        pop     {fp}
    8320:       e12fff1e        bx      lr
</pre>
<p>You can see that the Android GCC has done a much better job at stripping out the symbols. This is because of a subtle &#8220;.size&#8221; attribute given to functions in the assembly for Linux (and therefore Android) which tells the assembler how big the function is. However, this doesn&#8217;t exist on Mac and the size is calculated by taking the distance between the start of a symbol and the next symbol.</p>
<p>This actually helps us to understand a bit more what <strong>actually</strong> went wrong. If you look back up at the assembly generated for iOS and Android you&#8217;ll see that the &#8216;.loop&#8217; appears as a top level symbol, which is confirmed by running &#8216;nm&#8217; on the resulting object file. This sounds all wrong, since the .loop symbol should really be local to the &#8216;func()&#8217; function. The reason being because you need to prefix local symbols with &#8216;L&#8217;. If we change &#8216;.loop&#8217; for &#8216;Lloop&#8217; in the sample file, then these are the resulting outputs showing the iOS linker doing the right thing this time even with -dead_strip enabled.</p>
<p>Compiled output:</p>
<pre class="brush: plain; title: ; notranslate">
        .section __TEXT,__text,regular
        .section __TEXT,__textcoal_nt,coalesced
        .section __TEXT,__const_coal,coalesced
        .section __TEXT,__picsymbolstub4,symbol_stubs,none,16
        .text
        .align 2
        .globl _func
_func:
        @ args = 0, pretend = 0, frame = 4
        @ frame_needed = 1, uses_anonymous_args = 0
        stmfd   sp!, {r7, lr}
        add     r7, sp, #0
        sub     sp, sp, #4
        ldr     r3, [sp]
                mov r3, #10
Lloop:
        subs r3, r3, #1
        bne Lloop

        str     r3, [sp]
        sub     sp, r7, #0
        ldmfd   sp!, {r7, pc}
        .align 2
        .globl _funcB
_funcB:
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 1, uses_anonymous_args = 0
        stmfd   sp!, {r7, lr}
        add     r7, sp, #0
        ldmfd   sp!, {r7, pc}
        .subsections_via_symbols
</pre>
<p>Disassembly of linked application:</p>
<pre class="brush: plain; title: ; notranslate">
_main:
00002f8c        e92d4080        push    {r7, lr}
00002f90        e28d7000        add     r7, sp, #0      @ 0x0
00002f94        eb000002        bl      _func
00002f98        e3a03000        mov     r3, #0  @ 0x0
00002f9c        e1a00003        mov     r0, r3
00002fa0        e8bd8080        pop     {r7, pc}
_func:
00002fa4        e92d4080        push    {r7, lr}
00002fa8        e28d7000        add     r7, sp, #0      @ 0x0
00002fac        e24dd004        sub     sp, sp, #4      @ 0x4
00002fb0        e59d3000        ldr     r3, [sp]
00002fb4        e3a0300a        mov     r3, #10 @ 0xa
00002fb8        e2533001        subs    r3, r3, #1      @ 0x1
00002fbc        1afffffd        bne     0x2fb8
00002fc0        e58d3000        str     r3, [sp]
00002fc4        e247d000        sub     sp, r7, #0      @ 0x0
00002fc8        e8bd8080        pop     {r7, pc}
</pre>
<p>This all serves to illustrate the point that it&#8217;s worth knowing about the options of your compiler, assembler &#038; linker and understanding how everything fits together.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2011/03/assembly-beware-local-label-names-with-dead_strip-option/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Blocks in C++ classes &#8211; very broken</title>
		<link>http://iphone.galloway.me.uk/2010/11/blocks-in-c-classes-very-broken/</link>
		<comments>http://iphone.galloway.me.uk/2010/11/blocks-in-c-classes-very-broken/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 16:28:35 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=383</guid>
		<description><![CDATA[I was trying to use blocks (an Apple extension to the C language) in a C++ class when I came across a very strange problem. If you try to declare a block inside a C++ class member function that declares variables inside the block, then you end up with GCC (the one that comes with [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to use <a href="http://en.wikipedia.org/wiki/Blocks_%28C_language_extension%29">blocks</a> (an Apple extension to the C language) in a C++ class when I came across a very strange problem. If you try to declare a block inside a C++ class member function that declares variables inside the block, then you end up with GCC (the one that comes with the Mac/iOS SDK) throwing an error.</p>
<p>For instance, consider this code:</p>
<pre class="brush: cpp; title: ; notranslate">
class SomeClass {
public:
    SomeClass() {}
    ~SomeClass() {}

    void doSomething() {
        void (^block)(void) = ^{
            int a = 0;
        };
    }
};

int main (int argc, char * const argv[]) {
    return 0;
}
</pre>
<p>If you compile it, GCC will throw this error:</p>
<pre class="brush: plain; title: ; notranslate">
main.cpp: In function 'void __doSomething_block_invoke_1(void*)':
main.cpp:8: error: 'int SomeClass::a' is not a static member of 'class SomeClass'
</pre>
<p><del datetime="2010-11-04T17:36:23+00:00">Sadly there is no workaround that I can find as yet.</del></p>
<p>I have found a (not very nice) workaround:</p>
<pre class="brush: cpp; title: ; notranslate">
class SomeClass {
public:
    SomeClass() {}
    ~SomeClass() {}

    void doSomething() {
        void (^block)(void) = ^{
            int ::a;
            a = 0;
        };
    }
};

int main (int argc, char * const argv[]) {
    return 0;
}
</pre>
<p>Note that you <strong>cannot</strong> initialise the variable at the same time as declaring it inside the block. You must declare it, forcing global scope and then initialise it.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2010/11/blocks-in-c-classes-very-broken/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ARM Hacking: EXC_ARM_DA_ALIGN exception</title>
		<link>http://iphone.galloway.me.uk/2010/10/arm-hacking-exc_arm_da_align-exception/</link>
		<comments>http://iphone.galloway.me.uk/2010/10/arm-hacking-exc_arm_da_align-exception/#comments</comments>
		<pubDate>Wed, 06 Oct 2010 18:48:05 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=374</guid>
		<description><![CDATA[I came across a problem today that I&#8217;d seen before but couldn&#8217;t remember when. Then I stumbled across Peter Bakhirev writing up his findings at Byte Club about a problem I&#8217;d helped him with. So I thought I&#8217;d quickly write up my summary here incase it helps anyone else. It started back when I was [...]]]></description>
			<content:encoded><![CDATA[<p>I came across a problem today that I&#8217;d seen before but couldn&#8217;t remember when. Then I stumbled across Peter Bakhirev writing up his findings at <a href="http://byteclub.com/blog/44-development/85-memory-alignment-errors-on-iphone-os-3-2">Byte Club</a> about a problem I&#8217;d helped him with. So I thought I&#8217;d quickly write up my summary here incase it helps anyone else.</p>
<p>It started back when I was browsing the <a href="https://devforums.apple.com/message/209124">Apple developer forums</a> once &#8211; I came across someone having a problem where the EXC_ARM_DA_ALIGN exception was thrown. It turned out that this was a problem with setting the value of a variable by dereferencing a pointer, like so:</p>
<pre class="brush: cpp; title: ; notranslate">
char *mem = malloc(16); // alloc 16 bytes of data
double *dbl = mem + 2;
double set = 10.0;
*dbl = set;
</pre>
<p>The compiler was emitting a <a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0068b/BABEFCIB.html">STMIA instruction</a> that was using a memory location that isn&#8217;t word (32-bit) aligned (because of the +2 into the &#8216;mem&#8217; memory location &#8211; assuming mem is word aligned). This causes the processor to throw the exception that bubbles up as an EXC_ARM_DA_ALIGN.</p>
<p>The solution above would be to memcpy instead of dereferencing like so:</p>
<pre class="brush: cpp; title: ; notranslate">
char *mem = malloc(16); // alloc 16 bytes of data
double *dbl = mem + 2;
double set = 10.0;
memcpy(dbl, &amp;set, sizeof(set));
</pre>
<p>This is just a simple example of the problems that can occur when you&#8217;re not careful with reading/writing arbitrary memory that you have allocated. Granted, it&#8217;s probably a compiler bug in this case, but it illustrates the point.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2010/10/arm-hacking-exc_arm_da_align-exception/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Compiling Boost 1.44 for iPhone</title>
		<link>http://iphone.galloway.me.uk/2010/09/compiling-boost-1-44-for-iphone/</link>
		<comments>http://iphone.galloway.me.uk/2010/09/compiling-boost-1-44-for-iphone/#comments</comments>
		<pubDate>Mon, 27 Sep 2010 12:21:22 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=367</guid>
		<description><![CDATA[As a follow up to my other post about compiling Boost for iOS, here is how to do it for 1.44. First, edit your user-config.jam (i.e. edit ~/user-config.jam) to include the following: Then, follow these steps to install: [This will install boost into a prefix directory of ${HOME}/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator${SDK_VERSION}.sdk/usr]]]></description>
			<content:encoded><![CDATA[<p>As a follow up to my <a href="http://iphone.galloway.me.uk/2009/11/compiling-boost-for-the-iphone/">other post about compiling Boost for iOS</a>, here is how to do it for 1.44.</p>
<p>First, edit your user-config.jam (i.e. edit ~/user-config.jam) to include the following:</p>
<pre class="brush: bash; title: ; notranslate">
using darwin : 4.2.1~iphone
   : /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 -arch armv7 -mthumb -fvisibility=hidden -fvisibility-inlines-hidden
   : &lt;striper&gt;
   : &lt;architecture&gt;arm &lt;target-os&gt;iphone
   ;

using darwin : 4.2.1~iphonesim
   : /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -arch i386 -fvisibility=hidden -fvisibility-inlines-hidden
   : &lt;striper&gt;
   : &lt;architecture&gt;x86 &lt;target-os&gt;iphone
   ;</pre>
<p>Then, follow these steps to install:</p>
<pre class="brush: bash; title: ; notranslate">
# &lt;grab source from boost.org&gt;
tar xzf boost_1_44_0.tar.gz
cd boost_1_44_0
./bootstrap.sh

# Set this to whatever you want to build against
SDK_VERSION=&quot;4.1&quot;

# Install for device:
./bjam --prefix=${HOME}/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS${SDK_VERSION}.sdk/usr toolset=darwin architecture=arm target-os=iphone macosx-version=iphone-${SDK_VERSION} define=_LITTLE_ENDIAN link=static install

# Install for simulator
./bjam --prefix=${HOME}/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator${SDK_VERSION}.sdk/usr toolset=darwin architecture=x86 target-os=iphone macosx-version=iphonesim-${SDK_VERSION} link=static install
</pre>
<p>[This will install boost into a prefix directory of ${HOME}/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator${SDK_VERSION}.sdk/usr]</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2010/09/compiling-boost-1-44-for-iphone/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>iPhone Programming: The Big Nerd Ranch Guide</title>
		<link>http://iphone.galloway.me.uk/2010/05/iphone-programming-the-big-nerd-ranch-guide/</link>
		<comments>http://iphone.galloway.me.uk/2010/05/iphone-programming-the-big-nerd-ranch-guide/#comments</comments>
		<pubDate>Wed, 12 May 2010 15:42:19 +0000</pubDate>
		<dc:creator>Matt Galloway</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iphone.galloway.me.uk/?p=331</guid>
		<description><![CDATA[I got into Mac OS X / Cocoa / iPhone programming after reading the wonderful book by Aaron Hillegass called Cocoa Programming for Mac OS X and always thought that Big Nerd Ranch should do an iPhone book and, they have. I&#8217;ve had a flick through it and it looks like an excellent read. It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I got into Mac OS X / Cocoa / iPhone programming after reading the wonderful book by Aaron Hillegass called <a href="http://www.amazon.co.uk/Cocoa-Programming-Mac-OS-X/dp/0321503619/">Cocoa Programming for Mac OS X</a> and always thought that Big Nerd Ranch should do an iPhone book and, they have. I&#8217;ve had a flick through it and it looks like an excellent read. It&#8217;s called <a href="http://www.amazon.co.uk/IPhone-Programming-Ranch-Guide-Guides/dp/0321706242/">iPhone Programming: The Big Nerd Ranch Guide</a>. On the outset it looks to be a similar style to Cocoa Progamming for Mac OS X in that it talks about Objective-C and things you need to know there such as memory management and then goes on to describe the various parts of the iPhone SDK.</p>
<p>Anyway, I&#8217;d recommend buying it if you&#8217;re thinking about getting into iPhone programming.</p>
]]></content:encoded>
			<wfw:commentRss>http://iphone.galloway.me.uk/2010/05/iphone-programming-the-big-nerd-ranch-guide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

