Michael
Michael

Reputation: 1353

Gtkmm Segmentation Fault when presenting window

I am learning the gtkmm library and I ran straight into a brick wall.

I am using version 3.22.2.

This simple program that I wrote gets a seg fault when I call present on the main window and I can't figure out why.

I included a comment in the code below on the line that it segfaults.

#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();
}

Upvotes: 0

Views: 384

Answers (1)

No&#232; Murr
No&#232; Murr

Reputation: 626

there is no return value from the method createMainWindow. the pointer mainWindow in the on_active method is probably set to 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;
} 

Upvotes: 1

Related Questions