EXC_BAD_ACCESS when initialising NSDictionary

It’s been a while since I last paid this blog any attention. Since my last post I’ve changed job, country and had a haircut.

I’ve started toying with iOS applications and came across the EXC_BAD_ACCESS error fairly early on. Googling around tells me this is a fairly common error with all manner of causes, many of which stem from a misunderstanding of the memory management required when working is obj-c on iOS devices.

I’ll start by saying that I am a novice obj-c developer and have been spoilt for many years with modern languages that include garbage collection as standard. I understand pointers and reference and scalar variables but this knowledge has never been more pertinent than now.

I was initialising an NSDictionary as in the code snippet below. Content and username are NSString objects and type is an NSInteger, and there exactly is my error.

NSDictionary *post = [NSDictionary dictionaryWithObjectsAndKeys:
                        content, @"content",
                        username, @"user",
                        type, @"type",
                        nil];

NSDictionary stores objects (id types, or pointers to an object) with another object as the key, in this example all my keys are NSString objects. The error with my code is that type is NSInteger which is a scalar type. When attempting to store the NSInteger it is being interpreted as a pointer and therefore likely to point at an invalid memory location.

So that’s my understanding of the problem, what’s the solution?

Quite simple really, instead of storing a scalar type we need to store a pointer to an object, iOS provides the class NSNumber to solve the problem. So taking this into account the above code becomes:

NSDictionary *post = [NSDictionary dictionaryWithObjectsAndKeys:
                        content, @"content",
                        username, @"user",
                        [NSNumber numberWithInt:type], @"type",
                        nil];

and our EXC_BAD_ACCESS error goes away. I can tell my experience with objc and iOS is going to be a challenging but fun one!

Building POST data manually

After spending quite a while trying to figure this out I’m posting it as a reminder in future

POST http://example.com/upload.php HTTP/1.1
User-Agent: Photo Uploader (foobar/1.0.0)
Content-Type: multipart/form-data; boundary=AaB03x
Content-Length: xxxx 

--AaB03x
Content-Disposition: form-data; name="foo"
Content-Type: text/plain; charset=utf-8 

12345
--AaB03x
Content-Disposition: form-data; name="Filename"
Content-Type: text/plain; charset=utf-8 

image1.jpg
--AaB03x
Content-Disposition: form-data; name="MimeType"
Content-Type: text/plain; charset=utf-8 

image/jpeg
--AaB03x
Content-Disposition: form-data; name="ImageData";
filename="image1.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary 


--AaB03x--

Calculation errors with the scientific calculator on the iPhone 3G

I was playing around with the scientific calculator on my iPhone last night (don’t ask me why becuase I don’t really know) and found a repeatable error with a particular calculation, the cubed root of 64.

We all know that the cubed root of 64 is 4 (at least anyone likely to read my blog knows anyway) however the iPhone believes the cubed root of 64 is 4.000000000000001 which admittedly is pretty close to 4 but is strictly speaking still wrong.

Anyone care to shed some insight or ideas as to what might be causing this at a low level? I’m not looking for fixes, I’m not likely to use my iPhone for calculations that need that kind of accuracy but out of sheer curiosity I’d be interested to know how this could happen? Rounding errors in the code? How does one actually go about calculating the cubed root of a number? (it’s been a while since my math A-Level and I haven’t used it much since)

Answers on a postcard, or a comment below..