1

this is a self answering question. Putting it out there to help others.

I just had a really interesting crash with my app. What I wanted to do was to define a sort comparator that would be returned a static method in the model.

So, for example if my model is Car, and I want to sort by name, I would have in Car.m:

+ (NSComparator)nameSortAscendingComparator {
    NSComparator nameSortAscendingComparator = ^NSComparisonResult(id obj1, id obj2) {
        NSString *a = [(Car *)obj1 name];
        NSString *b = [(Car *)obj2 name] ;
        return [a compare:b] ;
    };
}

The invoking code looks something like this:

    NSComparator comparator = [Car nameSortAscendingComparator];
   _sortedCars = [cars sortedArrayUsingComparator:comparator];

Running this gave a real red herring exception. In fact, it looked something like this:

Xcode's main debug window screenshot

With the breakpoint stopped on:

Thread 1, Queue : com.apple.main-thread
#0  0x0231fcbc in _objc_empty_vtable ()
#1  0x0280c70a in __56-[NSArray sortedArrayFromRange:options:usingComparator:]_block_invoke_0 ()
#2  0x0273f07d in __CFSimpleMergeSort ()
#3  0x0273f124 in __CFSimpleMergeSort ()
#4  0x0273f008 in CFSortIndexes ()
#5  0x0277ad71 in -[NSArray sortedArrayFromRange:options:usingComparator:] ()
#6  0x0279a2b5 in -[NSArray sortedArrayUsingComparator:] ()

It led me up all sorts of garden paths. Could a block not be accessed as a static variable, or via a static method? Possibly it was doing all this in the initialize that was causing the problem?

Max MacLeod
  • 26,115
  • 13
  • 104
  • 132

1 Answers1

1

The give away was a compiler warning I failed to spot. As always, allowing compiler warnings to accumulate can be deadly. They obscure a real problem when it occurs.

In this case, it was:

Control reaches end of non-void function

Yep, my nameSortAscendingComparator method didn't actually return anything. Doh! Problem solved.

What I'd be curious about, would be the explanation behind the obscure "no such file" message? Not to mention the _objc_empty_vtable. I'd love to know more about the vtable, and it's relationship to the non-void function crash.

Max MacLeod
  • 26,115
  • 13
  • 104
  • 132