• Threading, a necessary evil

    Anyone I know that really has a clue avoids multi-threading programming (except for some server applications) as there are so many gotchas. Making things thread safe sounds easy, but is extremely hard as it is quite easy to overlook an item or two I got bit by this in ReceiptWallet in 2 spots. In one case, I build thumbnails on a separate thread to keep the main thread (where the user interacts) running. The problem is that if the user does something, i.e. remove a page or change metadata, I clear the underlying document in the main thread. So the secondary thread tries to use the document that no longer exists and quickly crashes. The fix wasn't difficult; I simply had to know when the secondary thread was running and don't change things on the main thread until the secondary thread exited. Of course, I could never reproduce this (like many thread related bugs, it is nearly impossible to track down the cause), so figuring this out was a bit problematic.

    The second thread related issue was a little easier to track down, but a bit less straightforward that I messed up. In this case, I used something like:

    	[self performSelector:@selector(saveChanges) withObject:nil afterDelay:0.0];
    

    and then proceeded to do:

    	NSTask *mdImportTask = [[NSTask alloc] init];
    	[mdImportTask setLaunchPath:@"/usr/bin/mdimport"];
    	[mdImportTask setArguments:[NSArray arrayWithObject:path]];
    	[mdImportTask launch];
    	[mdImportTask waitUntilExit];
    	[mdImportTask release];
    

    The problem here is that the waitUntilExit runs the main loop until the task (which is actually a separate thread) completes. So saveChanges actually fires when the task is running. The task is in a function that can get called by saveChanges so things become messy quite fast. I had to re-work some code to fix this.

    I worked on a project awhile ago that was heavily threaded and despite reassurances that the code was thread safe, it was a royal mess to track down bugs. While I use threads in a number applications I write, I write to avoid them as much as possible, except in cases where doing stuff on the main thread would make the UI unresponsive.

  • Cocoa bindings causing dealloc to not be called?

    In tracking down a bug in ReceiptWallet, I discovered some end (at least to me) odd behavior. ReceiptWallet is an NSDocument based application. When the document is closed, is should call dealloc to release its memory. This wasn't happening. It appears that some things in my code prevented dealloc from being called; I had to unbind some Cocoa bindings (not all of them, however):

    	[_receiptsArrayController unbind:@"selectionIndexes"];
    	[photoSizeSlider unbind:@"value"];
    	[self unbind:@"currentSelectedIndexes"];
    

    and then I had to remove some observers:

    	[collectionsTreeController removeObserver:self forKeyPath:@"selectionIndexPaths"];
    	[userDefaultsController removeObserver:self
    		forKeyPath:@"values.Show Details With Thumbnails"];
    

    and to top it off, I had to set the view on a toolbar item (a search item) to nil:

    	extern NSString *SearchToolbarItemIdentifier;
    	NSArray *items = [[[self window] toolbar] items];
    	NSEnumerator *itemEnumr = [items objectEnumerator];
    	NSToolbarItem *item = nil;
    	while (item = [itemEnumr nextObject])
    	{
    		if ([[item itemIdentifier] isEqualToString:SearchToolbarItemIdentifier])
    		{
    			[item setView:nil];
    			break;
    		}
    	}
    

    I can understand removing the observer for user defaults, but the bindings have me confused as the bindings are for items that should get released anyway. I'm probably missing something simple, but I'm glad I figured this one out.

  • Tracking down bugs

    One of the toughest parts of my job is tracking down bugs that either I create, are operating system bugs, or are a combination. Most of the bug reports I get are extremely incomplete and don't help me. I also get crash reports sent to me so that I can try to see the problems. With the crash reports, some users put in a sentence saying what they were doing. Unfortunately, this doesn't usually help me find the problem and fix it.

    Since releasing ReceiptWallet 2.0, I've gotten more crash reports than I would like; I've fixed some of the issues, but a few have eluded me. Last night I tackled reports dealing with rearranging collections; while I couldn't reproduce it (and no one provided enough information to help me), I reworked some code and am much happier with that chunk of code. Will it fix the problem? I'm not sure. Today I looked at another crash report dealing with turned on a preference. All of the reports related to this said "turned on show details with thumbnails". The crash reports made no sense to me as they were in Apple's code and not mine. I spent about 20 minutes trying a bunch of combinations to see if I could reproduce it. I was making no headway when I decided to open 2 libraries. Still no crash, then I closed one library, hit the preference and boom, crash. Yeah, I tried again and had the same result. So fixing it would be easy. It actually wasn't that hard to fix it, but when I started digging, I encountered another issue where closing a library never released the memory (dealloc wasn't being called), but that's another story.

    The more complete the bug report and having reproducible steps makes my life so much easier. For one project I had more than a decade ago, I was told "make this not crash". OK, easy, right? No, it took me 4 months of 8 hours a day to track down the issue (a bug in the ethernet driver). Was it worth it? Probably, I was working on a prototype system that was used to launch CDMA packet data.

  • Software is never done, it's just shipped

    In today's world of consumer software, the phrase "software is never done, it's just shipped" is the norm. Prior to the wide use of the Internet and downloadable updates, this wasn't always the case. When it cost real money to send out updates on media, software was tested more, but also people didn't expect updates as often. When I released ReceiptWallet 2.0 last week, I knew there was going to be an update. I released 2.0.1 this past Sunday and followed it up almost immediately with 2.0.2 b1 as I found more issues.

    This phrase may sound like a cop out, but I believe that it is the only way to write software. There are far too many variables to produce a product and have it work everywhere. There are some many different models of computers, various operating systems, hacks that modify the system, different applications that produce PDFs, etc. 10+ years ago, the combinations were far fewer, so it was easier to fully test a product. It is just not practical or even realistic to believe that anyone can ship consumer software that is bug free. Notice I said consumer software; if you have complete control (or significant control) over all the variables like the software in a microwave or a cordless phone, shipping bug free software is possible.

    Having said that, I personally like more updates rather than big, infrequent updates. As a developer, this lets me get out fixes to those that need them and as a user, I like to see that developers are actively working on the product.

    I'm a huge fan of the Sparkle framework and think that all programs should have this type of update mechanism built in. It makes it simple for users to get the updates and automatically install them. So, pushing out updates is quite easy.