我正在尝试在构造函数中的类中分配一个GtkWidget
指针,但是我遇到了运行时错误。
#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);
}
终端报告了以下错误:
(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
为什么这不会导致错误
GtkWidget* window = gtk_application_window_new(app);
但是这样做了?
appWindow = gtk_application_window_new(app);
更新:这是我的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;
}
我也在编译:g ++ pkg-config --cflags gtk+-3.0
-o main main.cpp pkg-config --libs gtk+-3.0
-std = c ++ 0x
答案 0 :(得分:2)
您没有显示完整的代码,但我认为问题出在这一行:
MainWindowController mainController(app);
在这里,您创建一个mainController
类型的局部变量MainWindowController
,然后立即销毁该变量,因为它超出了范围。
现在,接下来发生的事情的细节取决于这个类的析构函数的作用。我猜你正在对appWindow
做一些让对象无效的事情(gtk_widget_destroy()
可能吗?)。或者你可能在调用析构函数后尝试在信号中使用appWindow
,这是未定义的行为。