0

在我的应用程序中,我继承了 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 进行调试和构建。

4

1 回答 1

3

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

于 2013-07-08T12:48:35.693 回答