开发者

exc_bad_access crash in QT app

I'm writing a QT app and I'm very rusty with C++, so I'm guessing that's the problem. I've got a crash with an exc_bad_access signal on my Mac, whic开发者_StackOverflow社区h means I'm doing something wrong with memory. Here's my code:

void MainWindowController::showMainWindow() {
    MainWindow *w = mainWindow();
    w ->show();
}

MainWindow *MainWindowController::mainWindow() {
    if (NULL != _mainWindow)
        return _mainWindow;

    // otherwise, we need to load it and return it

    _mainWindow = new MainWindow(0);
    return _mainWindow;
}

_mainWindow is an instance variable, a pointer (as you might have guessed from the function signature). It's a simple lazy-loading. I think I'm doing memory management OK, as this class owns the object (which is later deleted in the destructor).

The crash occurs on the w -> show(); line, QT complains its somewhere inside the QWidget show() function, which doesn't really make sense to me.

Can somebody help me out? Thanks!


Turns out it was something even simpler. I'm used to Objective-C, where ivars are automatically initialized to 0. C++ doesn't do this. So, I had to make sure _mainWindow was initialized to NULL in the constructor. Problem solved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜