Archive for May 2011

objc_msgSend EXC_BAD_ACCESS error after moving operations to a GCD block

The subject says it all. After trying to improve the responsiveness of my app by using a Grand Central Dispatch block to run some code asynchronously, the code in my main queue GCD block was failing with an objc_msgSend EXC_BAD_ACCESS error. Bugger.

The error itself typically describes sending a message to an object that has been released and is no longer valid, but I am not sure how that could have happened just by moving some operations inside of GCD blocks.

The way that I fixed this was to go into the diagnostics tab in the Edit Scheme window, and I turned on the Enabled Guard Malloc option. Then, when I ran the app, it showed me more specifically where the issue was. The problem turned out to be that I was using an autorelease array that was becoming invalid as a result of going into and coming out of the GCD blocks. I switched the array to a retained object, and everything started working fine again.

Just don’t forget to turn off the Enable Guard Malloc when you are done testing.

And if you have not yet checked out the coolness of Grand Central Dispatch, you are missing out on a valuable tool.

UILabel sizeToFit malfunction

For some reason, my use of UIView’s sizeToFit method was not working as expected. What I got when I tried to send my UILabel controls through this method was that they ended up being sized down too far, and as a result text was either eliminated from the label, or an ellipsis appeared at the end. I tried just about every combination of UILineBreakMode and numberOfLines I could think of, with no combination fixing the problem.

So what I ended up doing was to just manipulate the UILabel’s frame in the table view’s cellForRowAtIndexPath method. Here is a code snippet:

if (feedIndex == SECTION_CUSTOMER_AND_JOB_INFO) 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_CUSTOMER_AND_JOB_INFO];
 
    if (cell == nil) 
    {
        [[NSBundle mainBundle] loadNibNamed:@"SummaryCustomerAndJobInfoCell" owner:self options:NULL];
        cell = cellCustomerAndJobInfo;
    }
 
    // the next line uses the tag property of the label as set in the nib file
    UILabel *lbl = (UILabel *)[cell viewWithTag:1];
 
    NSString *text = @"Set your multiple line\ntext to display here...";
    CGSize constraint = CGSizeMake(lbl.frame.size.width, 20000.0f);
    CGSize size = [text sizeWithFont:lbl.font constrainedToSize:constraint
                       lineBreakMode:UILineBreakModeWordWrap];
 
    lbl.text = text;
    [lbl setFrame:CGRectMake(lbl.frame.origin.x, lbl.frame.origin.y, 
                                      lbl.frame.size.width, size.height)];
 
    return cell;
}

By the way, don’t forget to do the same kind of size calculations in your heightForRowAtIndexPath delegate method.

This past Wednesday was a day of great sadness for me, as I had to put my 15 year old schipperke to sleep. Good bye Captain Kirk, you were an ornery little cuss but we loved you anyway. :'(

Just being thorough

I always get a little nauseous when looking through someone else’s code and seeing things like this:

res = sqlite3_exec(database, [select UTF8String], NULL, NULL, &errorMsg);
if (res != outcome) {
    [testStringCapture appendFormat:@"%@,",obj]; //changed 2/2/2011
}
else {
    [testStringCapture appendFormat:@"%@,",obj]; //changed 2/2/2011
}

Alas, a little later on in the same method was some code that soothed my sensibilities as it is kind a perfect fit into the whole “do something here” theme:

res1 = sqlite3_exec(database, [select1 UTF8String], NULL, NULL, &errorMsg1);
if (res1 != outcome1) {
 
}
else {
 
}

As per usual, these code snippets are unedited (except for some formatting) portions of production code.

Search bar and correction don’t play nice together

I just discovered something about the iOS platform that may not be readily apparent at first glance.

If you are using textDidChange method of a UISearchBar to interactively scan through your table’s data source (and let’s face it, who using the UISearchBar isn’t??), and you have the correction property of the search bar set to anything other than no, your textDidChange method can fire unexpectedly with a different searchTerm than you had expected, which can cause your table data to be out of sync.

BTW, Happy Mother’s Day to all mothers out there.

And an extra special Happy Birthday message to Joe Bonamassa. If you do not know who Joe Bonamassa is, feel shame for a few minutes, then go to iTunes, Amazon, Pandora, or wherever it is that you get music, punch up some of Joe’s music, and then turn it up loud. You’ll thank me later.

Stir Trek: Thor Edition, May 6, 2011

Today was the eagerly awaited Stir Trek event. I met some former co-workers at the Starbucks before the conference began, and then made my way over to the Marcus Crosswoods for the start of the conference.

The sessions were heavily .NET related, as you would expect from a conference put on by .NET folks. The sessions I attended were What’s new in ASP.NET MVC 3, NuGet, and MvcScaffolding by Jon Galloway, Github: Social Coding meets Enterprise Version Control by Joe O’Brien, A Crash Course in Windows Phone 7 Programming by Jesse Liberty, Multiplatform Physics games with Corona by Josh Smith, and Mobile Smackdown featuring Twitter apps courtesy of Jeff Fansler, Chris Judd, Justin Munger.

Following the sessions was a screening of the movie Thor. As one of my former co-workers tweeted after the movie, “What thor lacks in quality it makes up in volume”.

And by the way, thanks to all of you who downloaded my Stir Trek iPhone app (link redacted, app has been removed from the App Store). It was a quick and fun project to do.