Re: Dynamic widget creation/destruction



Russell Shaw wrote:

Hi,
When a bunch of widget objects are created such as in a dialog,
do they need to be destroyed to free memory before returning?
Does every widget need to be destroyed, or will destroying the
top widget automatically destroy all the child widgets? What
command to use for destroy?

static void
dialog_bg(void)
{
    GtkWidget* dialog=gtk_dialog_new_with_buttons("Grid Settings",NULL,
      GTK_DIALOG_MODAL|GTK_DESTROY_WITH_PARENT,
      GTK_STOCK_OK,GTK_RESPONSE_OK,
      GTK_STOCK_APPLY,GTK_RESPONSE_APPLY,
      GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL,
      NULL);

    GtkWidget *button=gtk_radio_button_new_with_label(NULL,"Grid");
    gtk_widget_show(button);
    GSList *group=gtk_radio_button_get_group(button);
    GtkWidget *button=gtk_radio_button_new_with_label(group,"Dots");
    gtk_widget_show(button);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
                        button, TRUE, TRUE, 0);
    // run modal
    gint result=gtk_dialog_run(dialog);
...
    < destroy things here??? >
}

They will be destroyed with a parent container.
When you create a widget it has a floating reference count, this stuff is well-explained here: http://developer.gnome.org/doc/API/2.0/gtk/GtkObject.html (search on word "floating")

You can remove some lines from the code above by using:
button = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(button), "Dots"); /* you dont need to handle radio group by hands */ gtk_widget_show_all(dialog); /* single call to show all children of container */

   Olexiy




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]