I’m writing a C game using the GTK library for the interface. The idea is to have two main containers: one for holding the buttons and other widgets for letting the user choose settings, and the other for displaying and moving the images during the actual gameplay. I’m using a VBox to hold the menu widgets, and a Fixed container to hold the game sprites. I have here all of the lines of code relating to the VBox and Fixed containers in the main() method:
GtkWidget* vbox;
GtkWidget* fixed;
...
int main(int argc, char** argv) {
// (The rest of this code block is in the main method)
...
// Make a vertical box for the menu widgets.
vbox = gtk_vbox_new(TRUE, 0);
// Add the menu widgets to the vbox.
gtk_box_pack_start(GTK_BOX(vbox), label1, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), button1, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), button2, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox), entry, TRUE, TRUE, 0);
// Make a new fixed container, which allows its children
// widgets to be moved dynamically.
fixed = gtk_fixed_new();
// Add the game widgets to the fixed container.
gtk_fixed_put(GTK_FIXED(fixed), player1, x, y);
gtk_fixed_put(GTK_FIXED(fixed), player2, x, y + 40);
gtk_fixed_put(GTK_FIXED(fixed), ball, x + 80, y);
gtk_fixed_put(GTK_FIXED(fixed), wall, x + 120, y);
// Add the fixed container to the window.
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_widget_show(vbox);
gtk_widget_show(fixed);
In one of my buttons’ actions, I have the following, to initiate gameplay:
gtk_container_remove(GTK_CONTAINER(window), vbox);
gtk_container_add(GTK_CONTAINER(window), fixed);
Which works without any issue. However, after a certain condition is met, I have another function called, which does the opposite:
gtk_container_remove(GTK_CONTAINER(window), fixed);
gtk_container_add(GTK_CONTAINER(window), vbox);
This causes the following issue:
(a.out:11762): Gtk-CRITICAL **: IA__gtk_container_add: assertion `GTK_IS_WIDGET (widget)' failed
I have looked at the widget hierarchy, and verified that VBox falls under the category of GtkWidget. The above error did not occur when I initially added the VBox to the window in the main() method. Is there something I’m not aware of when adding this container to the window after gtk_main() has been called?
The problem is taht GtkWidgets are reference counted but you don’t own any reference. This is usual for GtkWidgets, because as long as the widget is visible the window system holds one reference to them. But as long as you remove the widget from its container, the reference count drops to zero and it is destroyed.
The first time it works because there is a floating reference, that exist just after the widget is first created.
Your easiest solution is just to hold a reference to your two moving widgets: call
g_object_ref_sinkjust after creating them to convert the floating reference to a real one. But don’t forget to callg_object_unrefwhen you are done with them!Other option would be to just increment/decrement the counter while you are moving them around, but you still need to sink the reference to
fixedto work as expected:Then:
And: