Γραφικά συστατικά

25.1.1. Κανονική διαχείριση μνήμης C++

Η gtkmm επιτρέπει στον προγραμματιστή να ελέγξει τον χρόνο ζωής (δηλαδή, την κατασκευή και καταστροφή) οποιουδήποτε γραφικού συστατικού με τον ίδιο τρόπο όπως οποιοδήποτε άλλο αντικείμενο C++. Αυτή η ευελιξία επιτρέπει τη χρήση των new και delete για τη δημιουργία και καταστροφή αντικειμένων δυναμικά ή τη χρήση κανονικών μελών κλάσης (που καταστρέφονται αυτόματα όταν καταστρέφεται η κλάση) ή τη χρήση τοπικών στιγμιοτύπων (που καταστρέφονται όταν το στιγμιότυπο βγαίνει εκτός εμβέλειας). Αυτή η ευελιξία δεν παρουσιάζεται σε μερικά πακέτα εργαλείων GUI της C++, που περιορίζει τον προγραμματιστή σε ένα υποσύνολο μόνο των γνωρισμάτων διαχείρισης μνήμης της C++.

Ιδού μερικά παραδείγματα κανονικής διαχείρισης μνήμης C++:

25.1.1.1. Τα γραφικά συστατικά εμβέλειας κλάσης

Αν ένας προγραμματιστής δεν χρειάζεται δυναμική κατανομή μνήμης, μπορούν να χρησιμοποιηθούν αυτόματα γραφικά συστατικά στην εμβέλεια της κλάσης. Ένα πλεονέκτημα των αυτόματων γραφικών συστατικών σε εμβέλεια κλάσης είναι ότι η διαχείριση μνήμης ομαδοποιείται σε μια θέση. Ο προγραμματιστής δεν διακινδυνεύει διαρροές μνήμης από αποτυχία delete ενός γραφικού συστατικού.

Το κύριο μειονέκτημα χρήσης γραφικών συστατικών εμβέλειας κλάσης είναι η εμφάνιση της υλοποίησης κλάσης αντί για την διεπαφή κλάσης στην κεφαλίδα κλάσης.

#include <gtkmm/button.h>
#include <gtkmm/window.h>
class Foo : public Gtk::Window
{
private:
  Gtk::Button theButton;
  // θα καταστραφεί όταν το αντικείμενο Foo καταστραφεί
};

25.1.1.2. Γραφικά συστατικά εμβέλειας συνάρτησης

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();
}

25.1.1.3. Η δυναμική κατανομή με νέο και διαγραφή

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.

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

// do something useful with pButton

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

25.1.2. Διαχειριζόμενα γραφικά συστατικά

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::Container::add(), Gtk::Box::pack_start(), or a similar method. Now the widget will be destroyed whenever its container is destroyed.

25.1.2.1. Dynamic allocation with make_managed() and add()

gtkmm provides ways including the make_managed() function and Gtk::Container::add() 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()
{
  Gtk::Button* pButton = Gtk::make_managed<Gtk::Button>("Test");
  add(*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.

Φυσικά, ένας περιέκτης ανωτάτου επιπέδου δεν θα προστεθεί σε άλλον περιέκτη. Ο προγραμματιστής είναι υπεύθυνος για την καταστροφή του περιέκτη ανωτάτου επιπέδου χρησιμοποιώντας μία από τις παραδοσιακές τεχνικές της C++. Για παράδειγμα, το παράθυρο ανωτάτου επιπέδου σας μπορεί να είναι απλά ένα στιγμιότυπο στη συνάρτησή σας main().