Skip to content
  • Luca Bacci's avatar
    modern-gtk2: Delay construction of Gtk::TreeModel* objects (!17) · c602170f
    Luca Bacci authored and Mike Fleetwood's avatar Mike Fleetwood committed
    C++ initialises static member variables before main() is called.
    Therefore the static members of:
        struct Slots
        {
            static Gtk::TreeModelColumn<Glib::ustring> text;
            static Gtk::TreeModelColumn<bool> sensitive;
        private:
            static Gtk::TreeModel::ColumnRecord record_;
        };
    are constructed before Gtk::Main() is called in main().  However the
    Gtkmm documentation specifically says that they must be constructed
    afterwards [1].
    
    Resolve this by using the Construct On First Use Idiom [2] to delay
    initialisation until the slots are first used.  Normally this idiom uses
    static local objects, however it is being applied to class static
    objects here because the objects are accessed in many methods.  The
    downside of this approach is that the objects are never destructed,
    which memory analysers like Valgrind could see as a memory leak, but
    that is actually deliberate.  That leak can be removed once we can use
    C++11 and std::unique_ptr.
    
    [1] gtkmm: Gtk::TreeModelColumnRecord Class Reference
        https://developer.gnome.org/gtkmm/2.24/classGtk_1_1TreeModelColumnRecord.html#details
    
        "Neither TreeModel::ColumnRecord nor the TreeModelColumns contain
        any real data - they merely describe what C++ type is stored in
        which column of a TreeModel, and save you from having to repeat that
        type information in several places.
    
        Thus TreeModel::ColumnRecord can be made a singleton (as long as you
        make sure it's instantiated after Gtk::Main), even when creating
        multiple models from it.
        "
    
    [2] C++ FAQ / How do I prevent the "static initialization order
        problem"?
        https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
    
    Closes !17 - Gtk2 modernisation
    c602170f