Widgety

23.1.1. Normální správa paměti v C++

gtkmm dává programům kontrolu nad životností (tj. konstrukcí a destrukcí) kteréhokoliv widgetu stejným způsobme, jako nad kterýmkoliv jiným objektem C++. Díky této flexibilitě můžete použit new a delete k vytvoření a zničení objektu dynamicky, nebo můžete použít běžného člena třídy (který je zničen automaticky při zničení instance třídy), nebo použít lokální instanci (která zanikne při opuštění jejího rozsahu působnosti). Takovouto flexibilitu nenajdete v každé vývojářské sade GUI v C++, mnohé omezují programátory jen na nějakou podmnožinu funkcí správy paměti v C++.

Zde je nějaký příklad normální správy paměti v C++:

23.1.1.1. Widgety v rozsahu působnosti třídy

Pokud programátor nepotřebuje dynamickou alokaci paměti, dají se použít automatické widgety v rozsahu působnosti třídy. Jedním z přínosů automatických widgetů v rozsahu působnosti třídy je, že správa paměti je na jednom místě jako celek. Programátor neriskuje úniky paměti vlivem selhání delete u widgetu.

Hlavním přínosem použití widgetů v rozsahu působnosti třídy je odhalení implementace třídy, místo rozhraní třídy v hlavičce třídy.

#include <gtkmm/button.h>
#include <gtkmm/window.h>
class Foo : public Gtk::Window
{
private:
  Gtk::Button theButton;
  // bude zlikvidováno, když je zlikvidován objekt Foo
};

23.1.1.2. Widgety v rozsahu působnosti funkce

If a programmer does not need a class scope widget, a function scope widget may also be used. The advantages to function scope over class scope are the increased data hiding and reduced dependencies.

{
  Gtk::Button aButton;
  aButton.show();
  ...
  app->run();
}

23.1.1.3. Dynamická alokace pomocí new a delete

Usually, the programmer will prefer to allow containers to automatically destroy their children by creating them using Gtk::make_managed() (see below). This is not strictly required, as the new and delete operators may also be used, but modern C++ style discourages those in favour of safer models of memory management, so it is better to create widgets using Gtk::make_managed() and let their parent destroy them, than to manually perform dynamic allocation.

auto pButton = new Gtk::Button("Test");

// do something useful with pButton

delete pButton;
Here, the programmer deletes pButton to prevent a memory leak.

23.1.2. Widgety pod správou

Alternatively, you can let a widget's container control when the widget is destroyed. In most cases, you want a widget to last only as long as the container it is in. To delegate the management of a widget's lifetime to its container, create it with Gtk::make_managed() and then pack it into its container with Gtk::Box::append() or a similar method. Now the widget will be destroyed whenever its container is destroyed.

23.1.2.1. Dynamic allocation with make_managed() and append()

gtkmm provides ways including the make_managed() function and Gtk::Box::append() method to simplify creation and destruction of widgets whose lifetime can be managed by a parent.

Every widget except a top-level window must be added to a parent container in order to be displayed. The manage() function marks a widget so that when that widget is added to a parent container, said container becomes responsible for deleting the widget, meaning the user no longer needs to do so. The original way to create widgets whose lifetime is managed by their parent in this way was to call manage(), passing in the result of a new expression that created a dynamically allocated widget.

However, usually, when you create such a widget, you will already know that its parent container should be responsible for destroying it, In addition, modern C++ style discourages use of the new operator, which was required when passing a newly created widget to manage(). Therefore, gtkmm has added make_managed(), which combines creation and marking with manage() into a single step. This avoids you having to write new, which is discouraged in modern C++ style, and more clearly expresses intent to create a managed widget.

MyContainer::MyContainer()
{
  auto pButton = Gtk::make_managed<Gtk::Button>("Test");
  append(*pButton); //add *pButton to MyContainer
}

Now, when objects of type MyContainer are destroyed, the button will also be deleted. It is no longer necessary to delete pButton to free the button's memory; its deletion has been delegated to the MyContainer object.

Note that if you never added the widget to any parent container, or you did but later Gtk::Container::remove()d it from said parent, gtkmm restores the widget’s lifetime management to whatever state it had before manage() was called, which typically means that the responsibility for deleteing the widget returns to the user.

Kontejner nejvyšší úrovně samozřejmě nebude přidán do jiného kontejneru. Program je zodpovědný za zničení kontejneru nejvyšší úrovně pomocí jedné z tradičních technik C++. Například vaše okno nejvyšší úrovně může být jen instancí vaší funkce main().