gchapel
gchapel

Reputation: 11

Program throwing EXC_BAD_IINSTRUCTION error when exiting from a Swift tableView function

I am trying to recode in Swift an old objective-C program I wrote that reads various information from a BearCat scanner and displays it in a tableView. I created a class of "Scanner" to hold the scanner information to display, and have a dataArray that holds all the scanner objects with each object representing a hit of a frequency on the scanner. This is all written in a ViewController.swift file which also is the dataSource and delegate for the NSTableView object. Each column of my tableView has an identifier which matches one of the properties in the Scanner object. ie the Column that holds the "frequency" values has an identifier of frequency and the Scanner object has a property named "frequency". This valueForKey programming used to work in Objective-C but when I tried to write the same think in Swift it crashes with an error of

"Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" this is reported in the AppDelegate.swift file when it crashes.

Anyway here is my original Objective-C code which worked

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return [self.dataArray count];
}


- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    Scanner *s = [self.dataArray objectAtIndex:row];
    NSString *identifier = [tableColumn identifier];
    return [s valueForKey:identifier];
}

Here is the new swift code which fails

func numberOfRows(in tableView: NSTableView) -> Int {
        return dataArray.count
    }

    func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
       let scanObject = self.dataArray.object(at: row) as! Scanner
        let identifier = tableColumn!.identifier.rawValue
        return scanObject.value(forKey: identifier)
    }

Any ideas on how to fix this issue would be greatly appreciated as I am now lost. The crash is so abrupt when a new object is first loaded that print statements in the function don't make it to the console. It is crashing in the "return scanObject.value(forKey: identifier) line. The identifier appears to be the correct value for the first column in the tableView but it is a string and the function wants to return an Any type. I have tried setting breakpoints and analyzing the crash but I really don't see anything wrong and then boom it goes from the return line to the first line in AppDelegate.swift with the error above.

The AppDelegate file is unmodified from the original file that was generated when the project was created. I am sure there is probably a solution using core data but I am not using core data. I have my own dataArray which I manipulate on my own and supply the tableView using the ViewController as the data Source and delegate.

For those who can make sense of this here is some more crash info reported by AppDelegate

AppKit`-[NSApplication _crashOnException:]:
    0x7fff2fcb7ea9 <+0>:   pushq  %rbp
    0x7fff2fcb7eaa <+1>:   movq   %rsp, %rbp
    0x7fff2fcb7ead <+4>:   pushq  %r14
    0x7fff2fcb7eaf <+6>:   pushq  %rbx
    0x7fff2fcb7eb0 <+7>:   movq   %rdx, %rbx
    0x7fff2fcb7eb3 <+10>:  movq   0x59f49e66(%rip), %rsi    ; "description"
    0x7fff2fcb7eba <+17>:  movq   0x59c48977(%rip), %r14    ; (void *)0x00007fff6b3eb800: objc_msgSend
    0x7fff2fcb7ec1 <+24>:  movq   %rdx, %rdi
    0x7fff2fcb7ec4 <+27>:  callq  *%r14
    0x7fff2fcb7ec7 <+30>:  leaq   0x59c7c4fa(%rip), %rdi    ; @"Crashing on exception: %@"
    0x7fff2fcb7ece <+37>:  movq   %rax, %rsi
    0x7fff2fcb7ed1 <+40>:  xorl   %eax, %eax
    0x7fff2fcb7ed3 <+42>:  callq  0x7fff2fac1ddc            ; _NSNoteInCrashReports
    0x7fff2fcb7ed8 <+47>:  movq   0x59f4c1e1(%rip), %rsi    ; "callStackSymbols"
    0x7fff2fcb7edf <+54>:  movq   %rbx, %rdi
    0x7fff2fcb7ee2 <+57>:  callq  *%r14
    0x7fff2fcb7ee5 <+60>:  movq   0x59f4d5e4(%rip), %rsi    ; "componentsJoinedByString:"
    0x7fff2fcb7eec <+67>:  leaq   0x59c7b5d5(%rip), %rdx    ; @"'\n'"
    0x7fff2fcb7ef3 <+74>:  movq   %rax, %rdi
    0x7fff2fcb7ef6 <+77>:  callq  *0x59c4893c(%rip)         ; (void *)0x00007fff6b3eb800: objc_msgSend
    0x7fff2fcb7efc <+83>:  movq   0x59f49215(%rip), %rsi    ; "UTF8String"
    0x7fff2fcb7f03 <+90>:  movq   %rax, %rdi
    0x7fff2fcb7f06 <+93>:  callq  *0x59c4892c(%rip)         ; (void *)0x00007fff6b3eb800: objc_msgSend
    0x7fff2fcb7f0c <+99>:  movq   %rax, 0x59fc7d25(%rip)    ; gCRAnnotations + 24
->  0x7fff2fcb7f13 <+106>: ud2    
    0x7fff2fcb7f15 <+108>: movq   %rax, %rdi
    0x7fff2fcb7f18 <+111>: callq  0x7fff303f9d3a            ; symbol stub for: objc_begin_catch
    0x7fff2fcb7f1d <+116>: xorl   %edi, %edi
    0x7fff2fcb7f1f <+118>: xorl   %esi, %esi
    0x7fff2fcb7f21 <+120>: xorl   %eax, %eax
    0x7fff2fcb7f23 <+122>: callq  0x7fff2fac1ddc            ; _NSNoteInCrashReports
    0x7fff2fcb7f28 <+127>: callq  0x7fff303f9d6a            ; symbol stub for: objc_exception_rethrow
    0x7fff2fcb7f2d <+132>: ud2    
    0x7fff2fcb7f2f <+134>: movq   %rax, %rbx
    0x7fff2fcb7f32 <+137>: callq  0x7fff303f9d5e            ; symbol stub for: objc_end_catch
    0x7fff2fcb7f37 <+142>: movq   %rbx, %rdi
    0x7fff2fcb7f3a <+145>: callq  0x7fff303f96ec            ; symbol stub for: _Unwind_Resume
    0x7fff2fcb7f3f <+150>: ud2    
    0x7fff2fcb7f41 <+152>: callq  0x7fff303f9e3c            ; symbol stub for: objc_terminate


Thanks for any help!

Upvotes: 0

Views: 58

Answers (0)

Related Questions