【问题标题】:Gtkmm Segmentation Fault when presenting window呈现窗口时出现 Gtkmm 分段错误
【发布时间】:2018-01-19 20:22:39
【问题描述】:

我正在学习 gtkmm 库,我直接撞到了一堵砖墙。

我使用的是 3.22.2 版本。

当我在主窗口上调用 present 时,我编写的这个简单程序出现了 seg 错误,我不知道为什么。

我在下面代码中的段错误行中添加了注释。

#include <gtkmm.h>

using namespace Gtk;
using namespace std;

class App : public Application {
protected:
    App() : Application() {}

    void onWindowHide( Window *window ) { delete window; }

    void on_activate() override {
        ApplicationWindow *mainWindow = createMainWindow();
        mainWindow->present(); // it gets a SEG_FAULT here
    }

    ApplicationWindow *createMainWindow() {
        Gtk::ApplicationWindow *mainWindow;
        mainWindow = new ApplicationWindow();
        add_window( *mainWindow );
        mainWindow->signal_hide()
                  .connect( sigc::bind<Gtk::ApplicationWindow *>(
                          sigc::mem_fun( *this, &App::onWindowHide ), mainWindow ));
    }

public:
    static Glib::RefPtr<App> create() {
        return Glib::RefPtr<App>( new App());
    }
};


int main( int argc, char *argv[] ) {
    auto app = App::create();
    return app->run();
}

【问题讨论】:

    标签: segmentation-fault gtkmm gtkmm3


    【解决方案1】:

    createMainWindow 方法没有返回值。 on_active 方法中的指针 mainWindow 可能设置为 nullptr。

     ApplicationWindow *createMainWindow() {
        Gtk::ApplicationWindow *mainWindow;
        mainWindow = new ApplicationWindow();
        add_window( *mainWindow );
        mainWindow->signal_hide()
                  .connect( sigc::bind<Gtk::ApplicationWindow *>(
                          sigc::mem_fun( *this, &App::onWindowHide ), mainWindow ));
        return mainWindow;
    } 
    

    【讨论】:

    • 谢谢!那是一个相当愚蠢的错误。我很惊讶我的 IDE 没有捕捉到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 2020-09-10
    • 2012-02-28
    • 2018-07-11
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    相关资源
    最近更新 更多