I write a lot of code for ReceiptWallet as well as my day job doing contract work, so it goes without saying that I write some good code and some bad code. I like to think that I mostly write good code with a low bug count. I’ve been getting some really odd crash reports with ReceiptWallet and no one has actually sent a description of how they managed to crash it. I took a look today based on some guesses I had about how a customer was using the product and managed to track down the crash to one line of code; granted the crashes didn’t point to the line of code and were all across the board, but when I commented out the line, the funky behavior stopped. Turns out it was a really stupid bug; I use Cocoa Bindings in ReceiptWallet which saves code and is, in general, pretty neat. Basically in the UI, I specify that a particular field is bound to a variable. Then in the code, I set the variable. The trick is that the variable has to be set using Key Value Coding (KVC) such as:
[self setValue:[sender stringValue] forKey:@"variableName];
However, I found in my code a line that looked like this:
variableName = [sender stringValue];
When I first looked at it and started thinking about it, I thought I hadn’t retained the value and that the results were undefined when it exited the function, but after thinking about it all day, I realized my mistake, changed the code and presto, the crash was gone. What really threw me were crash reports like:
Thread 0 Crashed: 0 libobjc.A.dylib 0x94cc56e8 objc_msgSend + 24 1 com.apple.Foundation 0x95f4a180 -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 240 2 com.apple.Foundation 0x95f4a081 -[NSPredicate evaluateWithObject:] + 49 3 com.apple.CoreData 0x90c4520a -[NSManagedObjectContext executeFetchRequest:error:] + 2266
and
Thread 0 Crashed: 0 libobjc.A.dylib 0x94cc56e8 objc_msgSend + 24 1 com.apple.AppKit 0x936c3732 -[NSApplication run] + 892 2 com.apple.AppKit 0x936909ba NSApplicationMain + 574
Now I can sleep better knowing that I fixed a long standing bug that may have solved a number of crashes that I couldn’t explain.