Re: Must widgets stay in context?



R. Douglas Barbieri writes:
 > 
 > A question regarding this: does GTK have some sort of
 > garbage-collection management, or are all of the "manage()"ed widgets
 > finally freed at runtime?

I believe a managed widget is destroyed when it's container is.  This
is a pretty contrived example but shows that only the managed is destroyed
when the box holding it is ... 

one weird thing to note that no output is produced by the unmanaged
label's destructor, even when the application terminates in response
to the window being closed ...


#include <gtkmm/window.h>
#include <gtkmm/label.h>
#include <gtkmm/box.h>
#include <gtkmm/main.h>
#include <gtkmm/button.h>
#include <iostream>

Gtk::HBox *box1, *box2;

class Lbl : public Gtk::Label
{
public:

    Lbl(const Glib::ustring &caption)
        : Gtk::Label(caption)
    {
    }

    virtual ~Lbl()
    {
        std::cerr << get_text() << " is biting the dust\n";
    }
};

void DeleteLabelBoxes()
{
    std::cerr << "deleting both label boxes\n";
    delete box1;
    delete box2;
}

int
main(int argc, char *argv[])
{
    Gtk::Main kit(argc, argv);

    Gtk::Window win;
    Gtk::VBox vbox(true, 3);

    box1 = new Gtk::HBox(true, 0);
    Lbl *l1 = Gtk::manage(new Lbl("managed label"));
    box1->pack_start(*l1, Gtk::PACK_EXPAND_WIDGET);

    box2 = new Gtk::HBox(true, 0);
    Lbl *l2 = new Lbl("unmanaged label");
    box2->pack_start(*l2, Gtk::PACK_EXPAND_WIDGET);

    Gtk::Button btn("delete label boxes");
    btn.signal_clicked().connect(sigc::ptr_fun(DeleteLabelBoxes));

    vbox.pack_start(*box1, Gtk::PACK_SHRINK);
    vbox.pack_start(*box2, Gtk::PACK_SHRINK);
    vbox.pack_start(btn, Gtk::PACK_SHRINK);
    win.add(vbox);

    win.show_all_children();
    kit.run(win);

    return 0;
}




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