NSWindow keyDown:崩溃

时间:2013-07-08 11:48:58

标签: objective-c macos cocoa

在我的申请中,我继承了NSWindow课程。这是为了覆盖默认的按键事件。

//mywindow.h
@interface TWindow: NSWindow
{
}

- (void)keyDown: (NSEvent *)pEvent;
@end


//mywindow.mm
- (void)keyDown:(NSEvent *)theEvent
{
    //Detect Control-W and Control-Q and handle them.
    unsigned short  keycode;        ///< Key code.
    NSString *      charigmtch;     ///< Character ignoring matches.
    NSString *      character;
    BOOL            cmdkeydown;     ///< check if the command key is down.
    BOOL            shiftkeydown;   ///< Check if shift key is down.

    //Get the keycode of Control-W
    keycode     = [theEvent keyCode];

    //get character ignoring match.
    charigmtch  = [theEvent charactersIgnoringModifiers];

    //get character
    character   = [theEvent characters];

    cmdkeydown      = ([[NSApp currentEvent] modifierFlags] & NSCommandKeyMask) == NSCommandKeyMask;
    shiftkeydown    = ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask) == NSShiftKeyMask;
    //Get the keycode of Control
    if(cmdkeydown && 12 == keycode && ![character compare:@"q"] && ![charigmtch compare:@"q"]) {

        //CloseWithConfirm shows message box confirming quit.
        [self CloseWithConfirm];

    } else if (keycode == 48) {
        //Tab key is pressed.

            //This AppDelegate is application delegate and also NSWindowDelegate.
            AppDelegate *  delegate;       ///< Delegate.

        //Get the delegate from the window.
        delegate = (AppDelegate *)[self delegate];

        //Shift key is not down.
        if(!shiftkeydown) {
            //Tab key is pressed.
            [delegate TabKeyPressed];
        } else {
            //Shift-Tab key is pressed.
            [delegate ShiftTabKeyPressed];
        }
    }


    //Handle the other key.
    [super keyDown:theEvent];  //Line E
}

当我运行以下示例测试用例时:

void TestCase ()
{
    MyThread thread1, thread2, thread3, thread4;

    //Run thread 1
    thread1.Execute();

    //Run thread 2
    thread2.Execute();

    //Run thread 3
    thread3.Execute();

    //Run thread 4
    thread4.Execute();

}

//Following function is executed in thread function.
void Perform ()
{
    //This adds the data in table view.
    AddRowInTableView ("Test");
}

此代码大部分时间都会运行罚款。但有时,它会在代码中标记的E行崩溃。 我无法找到这次崩溃的原因。任何人都可以帮助我吗?

我正在使用Mac OS X Mavericks预览版和Xcode 5进行调试和构建。

1 个答案:

答案 0 :(得分:3)

不要在辅助线程中更新UI。必须在主线程上处理所有绘图和UI事件。