Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:36:26+00:00 2026-05-28T01:36:26+00:00

I’m writing a C game using the GTK library for the interface. The idea

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:36:26+00:00Added an answer on May 28, 2026 at 1:36 am

    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_sink just after creating them to convert the floating reference to a real one. But don’t forget to call g_object_unref when 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 fixed to work as expected:

    g_object_ref_sink(fixed);
    

    Then:

    g_object_ref(vbox);
    gtk_container_remove(GTK_CONTAINER(window), vbox);
    gtk_container_add(GTK_CONTAINER(window), fixed);
    g_object_unref(fixed);
    

    And:

    g_object_ref(fixed);
    gtk_container_remove(GTK_CONTAINER(window), fixed);
    gtk_container_add(GTK_CONTAINER(window), vbox);
    g_object_unref(vbox);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.