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!

Solve crackling and picking up radio transmissions with logitech x530 speakers

I recently got hold of a set of Logitech X530 5.1 speakers to replace the one speaker I had previously. They were cheap enough, looked alright and besides they had to be better than what I had! Or so I thought…

So I get home from work, speakers under arm, tear the box open, unpackage each speaker from its double bag wrapping (yay environment!) and route the cables around and behind my monitor. I get everything set up just as I want it plug them in, turn them on and, what’s that noise? A crackling Spanish radio station?! certainly sounds like it. I’m not great with deducing foreign languages but it definitely sounded like a european radio station was being received by my new speakers, what the hell?!

Anyway after a bit of tweaking dials and things I found that turning the volume up on the speakers themselves cut the crackling and radio reception completely so my solution is now to leave the volume up on the speakers and turn it way down in the operating system, bueno!

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

Upgrading project hamster to 2.25.3 from source

This post is an update to a previous post regarding project hamster.

Since installing the missing project hamster to my gnome 2.24 upgrade I have used it daily at work to track the time spent on each of the many projects I will participate in on any given day and it has been absolutely marvelous at helping me fill out my timesheets accurately.

However…

In addition to the amount of time I spend on a project and the name of the project, both of which are tracked superbly in 2.23,  I also have to enter a description of what I was doing for each period of time logged. It is at this point that I found project hamster lacking. Until now!

I’m not sure if it was in version 2.24 or 2.25 of hamster that the description field was added but it’s a very welcome addition.

Hamster 2.25 update activity dialog

Hamster 2.25 update activity dialog

When you update an activity in 2.25 you now have a description text area to store exactly that, a description! The even better way of inputting a description is with the single activity text box by splitting the activity and the description with a comma. i.e. “activity_name, here is what I did during this activity”

Additionally the single activity box can also be used to set the category of the activity by adding an @ symbol. i.e. “activity_name@Work, here is what I did during this activity” nifty eh?

So now that I’ve teased you with what the new version does here is a description of how to upgrade from source, it’s fairly straightforward especially if you’re familiar with building applications from source already.

1. In a terminal enter the following command which will install all the necessary dependencies for building hamster (the line below will cover wrap onto 3 lines though it should be entered in the terminal as a single line)

sudo apt-get install gnome-common python-pysqlite2 python-gtk2 python-gtk2-dev
python-gobject python-gobject-dev libgconf2-dev python-gnome2-dev
x11proto-scrnsaver-dev libxss-dev

2. Grab yourself a copy of the source from http://ftp.gnome.org/pub/GNOME/sources/hamster-applet/2.25/hamster-applet-2.25.3.tar.gz

3. Extract the source file to a folder, assuming you downloaded the source to your home directory you could extract it with the following commands

cd ~
tar -zxvf hamster-applet-2.25.3.tar.gz

4. Now the commands to actually build the plugin, again assuming you extracted the source in the line above to a folder named hamster-applet-2.25.3 in your home folder

cd ~/hamster-applet-2.25.3
./configure
make
sudo make install

5. And that’s it, should be rather painless, unless something goes horribly wrong (in which case leave a comment and I’ll see what I can do to help) All that’s left is to remove hamster from your panel and then add it again from the Add to panel dialog. Check that you’re running version 2.25.3 by right-clicking on the hamster applet and selecting About.

As always I hope that helps someone, comments are always appreciated.

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.

Failed VPN connections after upgrading to Ubuntu Intrepid Ibex 8.10

Another post concerning my upgrade experience with Ubuntu 8.10 this time with regards to VPN connections. I keep meaning to take the time to write a more comprehensive piece about the upgrade as a whole but for now I’ll keep posting little snippets of minor annoyances and how I solved them.

This problem appeared when I attempted to connect to a VPN for the first time since the upgrade. Firstly all of my saved VPN connections were gone, slightly annoying but understandable since the network manager has undertaken a major overhaul in this version. So I create a new VPN connection to a Windows 2003 Small Business Server I manage, I entered the host, username, password and domain and left everything else as default. I try to connect to the new connection and almost immediately an error message pops-up (certainly quicker than my machine could even resolve the hostname)

The precise error was “The vpn connection xxx failed because there were no valid VPN secrets” (not quite the precise error message since the connection wasn’t called xxx, having a VPN connection named xxx would conjure up unsavoury, NSFW thoughts…)

I won’t go into the whys and hows because I honestly haven’t had the time to find out but the answer for me was to go back and edit the VPN connection and remove the password. Next time I connected to the VPN I was prompted for the password and everything was back to normal and I can continue with my task of reading someone elses email mwah hahaha!

Ubuntu 8.10 upgrade experience

This is just a quick post concerning my upgrade to Ubuntu 8.10 Intrepid Ibex, there will be more to come later.

One of the new features I was most anticipating in this upgrade was the time tracking applet included with the upgrade to Gnome 2.24. I have to fill in daily time sheets at work and trying to keep track of what I work on in any one day has become a real hassle, I was hoping this applet would make completing these time sheets a lot easier so imagine my dismay when after performing the upgrade to 8.10 and opening the add to panel dialog I found no such applet!

Long story short and without explaination because I don’t know why it wasn’t there but I was able to add the time tracking applet by running the following in a terminal

sudo apt-get install hamster-applet

after installing hamster the time tracking applet was in my add to panel dialog and I am indeed looking forward to using it (apparently it took me 8 minutes to write this post!)

Moving your .ost file with Outlook 2003

Anyone that has read my blog before will know that I recently switched to Ubuntu as my primary OS however what I haven’t mentioned is that I also have a Windows XP virtual machine running for the sole purpose of running Outlook for my work email.

When I set up the virtual machine I was a bit too tight on the amount of space I gave to the primary hard disk image and have very quickly go to the point where Windows is complaining of a lack of space on drive C:

Firstly I moved the swap file to another virtual hard disk which solved the problem for a time but not for long. So I did some searching for files that were taking up space and found my Outlook offline cache file (outlook.ost) was fairly sizable so I wanted to move that too to the other virtual hard disk I’d set up, moving a file should be a straightforward task right? not if Microsoft has anything to do with it. Here are the steps you’ll need to follow in order to move your outlook.ost file.

  1. Close Outlook
  2. Move (or copy and delete the original at the end if you’re paranoid like me) the outlook.ost file from it’s current location (usually c:\documents and settings\{user}\Local Settings\Application Data\Microsoft\Outlook\outlook.ost) to a new location
  3. Open the mail settings applet either from the control panel or by right clicking on your Outlook link and selecting properties.
  4. Click E-mail Accounts…
  5. View or change and existing account and click next
  6. Highlight your exchange email account and click Change…
  7. Click More settings…
  8. Uncheck the Use Cached Exchange Mode and click apply
  9. Click Offline Folder File Settings…
  10. Click Disable Offline Use and agree with the pop up dialog message.
  11. This will close the Offline Folder File Settings dialog so click the button again
  12. You’ll find this time that the File: text box can now be edited. Click browse and locate the outlook.ost in its new location and click OK.
  13. Check the Use Cached Exchange Mode again and click OK.
  14. Click next and then finish.
  15. Close the mail settings applet and launch Outlook again.

Your outlook.ost file is now being used from it’s new location. Only 15 steps to move one file, and some of those steps I’ve doubled up on to be kind!

I understand that this has been improved in 2007 but I haven’t got a copy of that on hand to verify.

As always, I appreciate your comments. Let me know if this did or didn’t help you.

Better VNC viewer in Ubuntu 8.04

Another post regarding my recent switch to Ubuntu as my primary OS, this one concerns the VNC client/viewer bundled.  I’ve been having problems with Remote Desktop Viewer which is the bundled and preferred VNC client in a default installation of Ubuntu 8.04, I’ve had random freezes and disconnects with a logout/login of my machine sometimes resolving the issue temporarily.

The alternative is to use the Terminal Server Client application to connect to a VNC server, should just be a case of selecting VNC in the protocol dropdown right? nope, it’s greyed out*sigh*

To enable the VNC option in the Terminal Server Client dropdown you need to install the xtightvncviewer package either in Synaptic or with

sudo apt-get install xtightvncviewer

I fired up the Terminal Server Client, selected the VNC protocol, set the display to full screen and connected to my server.

Job done? not quite. I’m now prompted by my server with a “press ctrl-alt-del to logon” message. That’s where I had some problems, pressing ctrl-alt-del caused Ubuntu to ask me how I want to shut down. I needed some way of sending a ctrl-alt-del to the remote machine but I couldn’t see any obvious way of doing it. Queue the next problem, I’d selected full screen and that’s what I got, completely full screen, I couldn’t even switch to an alternative workspace since the VNC viewer remained on top.

I really didn’t want to restart my machine just because I couldn’t get rid of one window so I started randomly hitting buttons whereupon I found that F8 was rather useful! Pressing F8 with the VNC viewer focussed brings up a small menu with a number of options one of which was to toggle full screen and another was to send ctrl-alt-del to the remote machine, hooray!

I’ve found this VNC viewer to be a bit quicker at screen refreshes (though it does more of them) and more stable than the bundled viewer in Ubuntu, feel free to leave a comment to the contrary or suggesting another alternative that you may use.