Tracking down bugs

One of the hardest aspects of writing code is debugging it when it doesn’t want what you expect. In one of my past jobs, I was given the task of making a server stable which meant finding a bug buried in thousands of lines of code that I really didn’t understand. No one gave me a timetable on doing this, so I worked at it for over 4 months if I recall correctly. Turns out the problem was a bug in a driver that we didn’t even control; I put in a workaround and moved on.

Today I spent over 4 hours working with an excellent QA engineer to track down a bug; he did everything I asked him to do, sent me logs, reinstalled software, tried on a second machine, etc. Turns out that if I had read the log a little closer, I would have been able to fix the bug in minutes instead of hours. Having a QA resource like this makes it so much easier to find stupid bugs.

Stuffit Archives and .hqx encoded files

Stuffit products have been on my Macs for years and have done a wonderful job of compressing and decompressing. Stuffit Expander shipped with all versions of the Mac up until OS X Tiger. Now that zip is built into the OS and even has extensions for keeping the Mac resource fork (newer applications don’t use the resource fork anymore), I haven’t been able to come up with a reason to keep Stuffit Expander around except to deal with files I download that are still in .sit and .hqx formats. I find that some companies are still putting files on their websites with these extensions; if the app doesn’t have a resource fork, zip it. If it does or you want to customize the look, use a disc image (.dmg) and let a user download that. I think by now users are used to using disc images. Also, the disc image doesn’t need to be zipped or .bin put on the end; just make sure your web server serves up the .dmg file as application/octet-stream.

Stupid Patents (including software patents)

Today I read that Cingular has patented a way of generating an Emoticon (smilies, etc.). This seems insane that a patent be granted on something that has been in use for years. Cingular may have put a slight spin on it, but the Patent and Trademark Office doesn’t seem to have a clue about what constitutes a new idea. Speaking of patents, I’m not a fan of software patents as a simple idea that a developer codes up one day thinking that he is clever only to find out that he has violated a patent. If I patented every idea I came up with when writing code, I would never have time to actually write code as I solve problems and it really doesn’t matter how I do it in code, so I use lots of different techniques in order to get the job done. Hopefully the Patent and Trademark Office will learn to only patent really clever ideas and not stuff made up to extort money from others.

Cocoa NSAutoreleasePool

Autorelease pools are extremely convenient for not having to worry about memory allocation and freeing the allocated memory, similar to the Newton days with its garbage collection. A problem that I’ve been aware of with autorelease pools, but promptly forget all the time is that if you are in a tight loop and keep creating autoreleased items, you’re going to run out of memory and crash. For instance, in the following block of code, without even realizing it, a new autoreleased NSDate is being created once every tenth of a second. If you are processing data for say 3 minutes, you have created 1800 of these that aren’t being released.

while (running)
{
	[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.10]];
}

Now that I’ve thought about this, 1800 doesn’t sound all that bad, however, if you keep doing this without giving the OS a chance to do the autorelease cleanup, you’ll run out of memory. A better way of doing this is:

while (running)
{
	NSDate *newDate = [[NSDate alloc] initWithTimeIntervalSinceNow:0.10];
	[NSThread sleepUntilDate:newDate];
	[newDate release];
}

This way you won’t have anything lying around. Hopefully this will save a crash or two and lots of time scratching my head figuring out why it is crashing when I can’t reproduce the problem.

Bluetooth and the Intel iMac

After I setup the new iMac, I started testing it. One of the things I did was try to send a file from a Palm to the iMac via Bluetooth. Hmmm…didn’t work. Start thinking about this and turn off the Bluetooth keyboard and mouse. Presto, the file transferred without problems. Turned keyboard and mouse back on and it stopped working. This doesn’t bode well for our software; people already blame us for every problem on their machine, now I’m sure someone will say they can’t sync with a Bluetooth keyboard and mouse even though it is quite possibly not our problem. Lovely.

Other People’s Code – Open Source

I think that open source software (with license agreements that let me use it in commercial applications even if it means contributing my changes back) is great. We use it extensively in my work and have contributed back our changes as required in one component. Another component we use has proven to be a constant thorn in my side that I’m almost at the point that I’m going to scrap the code and write it myself; however, it does stuff that I don’t know how to do. I’ve had to fix this particular bunch of code twice in the last week. On a positive note, it keeps me employed.

Bluetooth Vodoo

I think that the concept of Bluetooth is great; allow devices to connect without wires. While there are standards on what to do, in my experience it just doesn’t work right all the time. With my cell phone, I still can’t get a headset to function properly. In dealing with software, I spent the last several days tracking down an issue customers were having with our software. As far as I can tell, it was Apple’s implementation of Bluetooth that got messed up on some people’s machines which caused our stuff to stop working. Of course, people blame us. Some day maybe I won’t have to fight technology to use it.

WirelessModem up for sale

After a lot of thought, I’ve decided to put my WirelessModem program up for sale. I’ve neglected the software for too long due to my own lack of interest (I haven’t used the software since I got rid of my Treo 300 a few years back). If anyone is interested in purchasing the source code to both the Macintosh side and the Palm OS side, please contact me. Serious offers only.

Voicemail notification

Whenever we get voicemail at home, MCI sends an email to whatever address I specify. This is great, but I don’t recognize most phone numbers. So, today I got the great idea to write a script that takes the inbound number, looks it up in an SQL database, and then sends the name to my cell phone. This is extremely cool; then I was able to import the names from my address book into the database. I learned out php, sed, and SQL in this particular exercise.I’m glad that I have the knowledge to learn whatever I need to in order to make technology work for me.

I used 2 scripts, the first is called from /etc/mail/aliases as in:

vm_notify:              "|/usr/local/bin/vmnotify_stage1",

Where vmnotify_stage1 is:

#!/bin/sh
sed '
{
        /You have received/!d
        h
        s/You have received a Voicemail message from //
        h
        s/.//

}' | /usr/local/bin/vmnotify_stage2

and

vmnotify_stage2 is:

#!/usr/bin/php -q 
<?php
$server = "localhost";  // server to connect to.
$database = "contacts";       // the name of the database.
$db_user = "username";        // mysql username to access the database with.
$db_pass = "password";   // mysql password to access the database with.
$table = "callerid";       // database table

$phone_number = trim(fgets(STDIN)); // reads one line from STDIN
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

$result = mysql_query("SELECT * FROM $table WHERE `phone_number` like "%$phone_number%" ORDER BY `phone_number`", $link)
or die ("Could not read data because ".mysql_error());

$text = "";
$num_rows = mysql_num_rows($result);
if ($num_rows > 0)
{
        while ($qry = mysql_fetch_array($result))
        {
                $text = $text . "$qry[name] ($qry[phone_number])n";
        }
}
else
{
        $text = $phone_number;
}

mail("example@example.com","Voicemail from:", $text);

mysql_close();

?>

This may not be the most efficient way, but it appears to work.

Crashes, crashes, crashes

One thing I really hate about releasing software is that no matter how much testing you do, it is bound to crash on users. Now the problem is, how can I reproduce the crashes and fix the issues? Users swear they can reproduce the issues, but I usually can’t no matter how much I stand on my head and type with my feet. There must be a better way to extract data from customers and troubleshoot issues.