Search code examples
gtk2

How to fix "assertion 'GDK_IS_SCREEN (screen)' failed" error in GTK+2


I'm trying to write a short C program, that makes all programs reload the GTK+2 theme, the way that LXAppearance does, when you click on apply.

Looking at LXAppearance's source code I found a function that does just that. The same function (without an if-else that only applies to LXSession) is also found in gtk-theme-switch.

This is the code written as a separate program:

#include <gtk/gtk.h>

int main()
{
    GdkEventClient event;
    event.type = GDK_CLIENT_EVENT;
    event.send_event = TRUE;
    event.window = NULL;
    event.message_type = gdk_atom_intern("_GTK_READ_RCFILES", FALSE);
    event.data_format = 8;
    gdk_event_send_clientmessage_toall((GdkEvent*)&event);
}

When running the program I the following error:

(process:3428): Gdk-CRITICAL **: 21:38:49.599: IA__gdk_screen_get_root_window: assertion 'GDK_IS_SCREEN (screen)' failed
Segmentation fault (core dumped)

What am I doing wrong?


Solution

  • You've not initialized GTK. At the very least, to access your current X display with GTK, you need to call the gtk_init function before doing any other X-related things.