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

Alternative method for opening Smarty templates with HTML syntax highlighting in gedit

Here is an update to a previous post regarding how to get gedit to recognise Smarty template *.tpl files. I’ve found that gedit prefers to use the mime type to decide what type of file it is and therefore what syntax highlighting to apply.

After a little investigation I found that most of my .tpl files were being identified as text/plain in Nautilus (found by right-clicking on the file and selecting properties, on the basic tab the type field will tell you the mime type nautilus is seeing it as)

My new solution to this problem is to convince nautilus (and gedit) that the *.tpl files are in fact HTML files and therefore syntax highlight them in the same way, and here’s how to do it:

  1. Make a backup of your freedesktop.org.xml file with the following commands:

    cd /usr/share/mime/packages
    sudo cp freedesktop.org.xml freedesktop.org.xml.backup

  2. Edit the freedesktop.org.xml file as a super user:

    sudo gedit /usr/share/mime/packages/freedesktop.org.xml

  3. Search the file for “*.php” (without the quotes). You should find a section with:

    <glob pattern="*.php"/>
    <glob pattern="*.php3"/>
    <glob pattern="*.php4"/>

  4. Add a new glob entry for tpl files by adding the line:

    <glob pattern="*.tpl"/>

  5. Save and close the file then run the following command which should be fairly self-explanatory:

    sudo update-mime-database /usr/share/mime

  6. Nautilus and gedit should now recognise .tpl files as html files and syntax highlight as such

The only drawback with this option (and I consider it a rather minor inconvenience) is that double clicking your tpl files will try and open them in Firefox by default now, you can change this to open in gedit by default but then all your .html and .htm files will open in gedit by default too.

You choose which you prefer, I personally have no problem leaving them to open in Firefox by default since I rarely double click tpl files in nautilus, I usually use the file browser plugin in gedit.

Thanks to a suggestion by Sudhanshu I’ve edited the post to add the tpl entry to the php section of the freedesktop.org.xml file instead of the html section. The benefit of this is that you can now double click tpl files and have them open in the same application as php files (usually gedit) while html files will still open in your browser.

As always comments are appreciated on whether this was helpful or if you’ve found a better way.

YouTube GData API returning error 400 when uploading a video

I’ve been battling with the YouTube API for the last 2 days now and found the single line of code that has been preventing me from generating a form upload token.

I was following the example code at http://code.google.com/apis/youtube/developers_guide_php.html#UploadingVideos and kept receiving “Expected response code 200, got 400″ when calling the getFormUploadToken function. HTTP error code 400 is a bad request which wasn’t really much help.

Eventually I found the problem and I will own up straight away that it was basically my fault, I’d made what I considered to be a slight change to the example code, I changed

$mediaGroup->category = array(  $yt->newMediaCategory()->setText('Autos')->setScheme
('http://gdata.youtube.com/schemas/2007/categories.cat'),

$yt->newMediaCategory()->setText('mydevelopertag')->setScheme
('http://gdata.youtube.com/schemas/2007/developertags.cat'),

$yt->newMediaCategory()->setText('anotherdevelopertag')->setScheme
('http://gdata.youtube.com/schemas/2007/developertags.cat')

);

to the following

$mediaGroup->category = $yt->newMediaCategory()->setText('Autos')->setScheme
('http://gdata.youtube.com/schemas/2007/categories.cat');

because I wasn’t interested in multiple categories, just something simple. My mistake was not in removing the two developer tags but removing the array(). This took me almost an entire day to realise and resulted in a “You have to be kidding” comment from me once fixed. *Sigh* oh well, not going to get that time back but at least I learned not to mess with example code.

Smarty template syntax highlighting with gedit

Update: I have posted an alternative way to do the same thing this post describes, the alternative way should be more reliable. See it here http://www.c3b.co.uk/2008/12/16/alternative-method-for-opening-smarty-templates-with-html-syntax-highlighting-in-gedit/

I’ve started using gedit as my code editor after a couple of niggles with bluefish and wanted my smarty templates (*.tpl) to use the same syntax highlighting as HTML. Not wanting to have to manually select the language each time I open a .tpl file I went searching for a way to add them to the definition of what an html file is. I was getting frustrated with all the references to a syntax highlighting tab in the preferences dialog that just didn’t exist! Even the help file in gedit 2.22.1 still mentions the syntax highlighting tab but I can assure you it’s not there.

The solution I came up with was to manually edit the HTML language definition file for gtksourceview which is what gedit uses to define the syntax highlighting. To achieve this you must change the following line in the /usr/share/gtksourceview-2.0/language-specs/html.lang file:

<property name="globs">*.html;*.htm</property>

to

<property name="globs">*.html;*.htm;*.tpl</property>

Or download my version here (html.lang) of the file and run the following command:

Backup existing file

cd /usr/share/gtksourceview-2.0/language-specs
sudo cp html.lang html.lang_bkup

Then move the downloaded version of the file into place (this assumes you download the file and extracted it to your home directory)

sudo mv ~/html.lang /usr/share/gtksourceview-2.0/language-specs/html.lang

Restart gedit and when you next open a .tpl file it will be highlighted in the same way as an HTML file.

I’ve finally embraced CSS

I’ve been putting together sites for years but never really taken the time to learn CSS properly and sticking with table based structure for far too long, finally I have built my first site using nothing but CSS and div tags for layout. It’s probably not perfect but it works and that’s the main thing for me.

The site in question is http://www.cazzbuckley.co.uk

With my new found confidence in CSS I will now attempt to create a WordPress theme for that same site’s blog section, let’s see how it goes…

<fb:share-button /> useless error message

It seems the <fb:share-button> control has 1 error message:

There was an error trying to share this content.
Sorry, the content owner's privacy settings don't allow you to share
this content.

The actual reason for this error message was that the href of the target_url link was blank. It turns out this can happen if you mistype target_url too.