0

I am trying to assign a GtkWidget pointer in my class within the constructor, however I am encountering run-time errors.

#include <gtk/gtk.h>

class MainWindowController
{
private:
  GtkWidget * appWindow;
  const gchar * windowTitle = "Window title";
public:
  MainWindowController(GtkApplication * app);
  ~MainWindowController();
  void show();
};

MainWindowController::MainWindowController(GtkApplication* app)
{
  //this works
  GtkWidget* window = gtk_application_window_new(app);

  //not this 
  //appWindow = gtk_application_window_new(app);

  /*adding it into the init list does not work either... I have tried
   MainWindowController::MainWindowController(GtkApplication* app)
      :appWindow(gtk_application_window_new(app)) {}
  */

  gtk_window_set_title(GTK_WINDOW(appWindow), windowTitle);
  gtk_window_set_default_size(GTK_WINDOW(appWindow), 500, 500);
}

The following errors are being reported at the terminal:

(main:156): GLib-GObject-WARNING **: instance with invalid (NULL) class pointer
(main:156): GLib-GObject-CRITICAL **: g_signal_handlers_destroy: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed
(main:156): GLib-GObject-WARNING **: instance with invalid (NULL) class pointer
(main:156): GLib-GObject-CRITICAL **: g_signal_handlers_destroy: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

Why doesn't this cause an error

GtkWidget* window = gtk_application_window_new(app);

but this does?

appWindow = gtk_application_window_new(app);

Update: Here is my main.cpp

#include <gtk/gtk.h>
#include "mainWindowController.hpp"

static void
activate (GtkApplication* app,
          gpointer        user_data)
{
  MainWindowController mainController(app);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), 0, 0);
  g_object_unref (app);

  return status;
}

I am also compiling with: g++ pkg-config --cflags gtk+-3.0 -o main main.cpp pkg-config --libs gtk+-3.0 -std=c++0x

1
  • We'll need to see more code. Nothing seems wrong with what you posted. Commented Dec 27, 2016 at 20:05

1 Answer 1

2

You are not showing the full code, but I think that the problem is in this line:

MainWindowController mainController(app);

Here you create a local variable mainController of type MainWindowController and immediately after that, the variable is destroyed, as it goes out of scope.

Now, the details of what happens next depend on what the destructor of this class does. I'm guessing that you are doing something to appWindow that makes the object invalid (gtk_widget_destroy() maybe?). Or maybe you are trying to use the appWindow in a signal after the destructor is called, and that is undefined behavior.

Sign up to request clarification or add additional context in comments.

1 Comment

Spot on! Was my destructor, I was calling g_object_unref(appWindow)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.