IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

GTK+ avec C & C++ Discussion :

Destruction d'un widget


Sujet :

GTK+ avec C & C++

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé Avatar de Gamall
    Profil pro
    Étudiant ENSEA
    Inscrit en
    Août 2009
    Messages
    252
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant ENSEA

    Informations forums :
    Inscription : Août 2009
    Messages : 252
    Par défaut Destruction d'un widget
    Bonjour,

    J'aimerais savoir quand est-ce que les GtkWidgets sont détruits dans un programme.
    J'ai un widget YaogcFrame qui dérive (directement) de GtkDrawingArea, celui-ci contient une donnée membre pango_l que je veux détruire en même temps que mon widget.

    J'ai donc fait ceci:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    static void
    yaogc_frame_finalize (GObject *object)
    {
        YaogcFrame *frame = YAOGC_FRAME (object);
     
        if (frame->priv->pango_l) {
            g_object_unref (frame->priv->pango_l);
            frame->priv->pango_l = NULL;
        }
     
        /* Chaining up */
        G_OBJECT_CLASS (yaogc_frame_parent_class)->finalize (object);
    }
     
    static void
    yaogc_frame_class_init (YaogcFrameClass *frame_class)
    {
        GObjectClass *object_class = G_OBJECT_CLASS (frame_class);
     
        /* [...] */
     
        object_class->finalize = yaogc_frame_finalize;    
     
        /* [...] */
    }
    J'ai placé un breakpoint dans la fonction yaogc_frame_finalize, et à la fin du programme celle-ci n'est jamais appelée, sauf si je fais appel explicitement à g_object_unref.

    Du coup, je me demande quand est-ce que les widgets sont détruits dans un programme

    Si quelqu'un a une idée, ça serait sympa
    Merci

    References: Object destruction

  2. #2
    Modérateur

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2009
    Messages
    1 395
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2009
    Messages : 1 395
    Par défaut
    Tu ne nous montres pas la partie la plus importante: quand ton widget est instancié. Autrement, un widget est à ma connaissance détruit avec g_object_unref (et que le compteur de références atteint 0), ou quand son parent est détruit.

  3. #3
    Membre confirmé Avatar de Gamall
    Profil pro
    Étudiant ENSEA
    Inscrit en
    Août 2009
    Messages
    252
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant ENSEA

    Informations forums :
    Inscription : Août 2009
    Messages : 252
    Par défaut
    En fait, j'avais testé en mettant mon YaogcFrame dans une YaogcWindow (qui hérite de GtkWindow), et c'est là que le programme ne rentre pas dans la routine yaogc_frame_finalize.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    int main (int argc, char ** argv)
    {
        GtkWidget *window;
        GtkWidget *frame;
     
        gtk_init (&argc, &argv);
     
        /* Création de la YaogcWindow */
        window = yaogc_window_new ();
     
        /* Création du YaogcFrame, et ajout dans la zone de travail de la YaogcWindow */
        frame = (GtkWidget*)g_object_new (YAOGC_TYPE_FRAME, NULL);
        gtk_box_pack_start (GTK_BOX(YAOGC_WINDOW(window)->work_area), frame, TRUE, TRUE, 0);
     
        /* Affichage de la YaogcWindow et son contenu */
        gtk_widget_show_all (window);
     
        gtk_main ();
     
        return 0;
    }
    Mais par contre,je viens de tester avec une GtkWindow, et là, le programme rentre dans la routine finalize, sans appel explicite à g_object_unref.

  4. #4
    Membre confirmé Avatar de Gamall
    Profil pro
    Étudiant ENSEA
    Inscrit en
    Août 2009
    Messages
    252
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant ENSEA

    Informations forums :
    Inscription : Août 2009
    Messages : 252
    Par défaut
    Par contre, si je mets un

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    g_object_unref (window)
     
    return 0;
    La YaogcWindow est détriute et le YaogcFrame qui est dedans aussi, car comme tu l'as dit, quand le widget parent est détruit, le widget enfant est détruit aussi.

    Du coup, on en revient au même problème, pourquoi la YaogcWindow (qui dérive de GtkWindow) n'est pas détruite automatiquement, alors que les GtkWindow le sont

  5. #5
    Membre confirmé Avatar de Gamall
    Profil pro
    Étudiant ENSEA
    Inscrit en
    Août 2009
    Messages
    252
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant ENSEA

    Informations forums :
    Inscription : Août 2009
    Messages : 252
    Par défaut
    Et voici le code de YaogcWindow si ça interesse quelqu'un:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    /* yaogc-window.h */
     
    #ifndef __YAOGC_WINDOW_H__
    #define __YAOGC_WINDOW_H__
     
    #include <glib.h>
    #include <glib-object.h>
    #include <gtk/gtk.h>
     
    G_BEGIN_DECLS
     
    #define YAOGC_TYPE_WINDOW (yaogc_window_get_type())
    #define YAOGC_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), YAOGC_TYPE_WINDOW, YaogcWindow))
    #define YAOGC_IS_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), YAOGC_TYPE_WINDOW))
    #define YAOGC_WINDOW_CLASS(klass) (G_TYPE_CLASS_CHECK_CAST((klass), YAOGC_TYPE_WINDOW, YaogcWindowClass))
    #define YAOGC_IS_WINDOW_CLASS(klass) (G_TYPE_CLASS_CHECK_CAST((klass), YAOGC_TYPE_WINDOW))
    #define YAOGC_WINDOW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), YAOGC_TYPE_WINDOW, YaogcWindowClass))
     
    typedef struct _YaogcWindow YaogcWindow;
    typedef struct _YaogcWindowClass YaogcWindowClass;
    typedef struct _YaogcWindowPrivate YaogcWindowPrivate;
     
    struct _YaogcWindow {
        GtkWindow parent_instance;
     
    	/* Put your widgets here */
        GtkWidget *work_area;
     
    	/* Private members */
        YaogcWindowPrivate *priv;
    };
     
    struct _YaogcWindowClass {
        GtkWindowClass parent_class;
     
    };
     
    GType yaogc_window_get_type (void) G_GNUC_CONST;
     
    GtkWidget * yaogc_window_new (void);
     
    G_END_DECLS
     
    #endif  /* !__YAOGC_WINDOW_H__ */
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    /* yaogc-window.c */
     
    #include <glib.h>
    #include <glib-object.h>
    #include <gtk/gtk.h>
    #include "yaogc-window.h"
     
    #define __YAOGC_WINDOW_XML___   "yaogc-window.xml"
     
    struct _YaogcWindowPrivate {
        GtkUIManager *manager;
        GtkActionGroup *action_group;
     
        GtkWidget *menu_bar;
        GtkWidget *dialog_quit;
    };
     
    /* Define new type YaogcWindow, create yaogc_window_get_type
     * and yaogc_window_parent
     */
    G_DEFINE_TYPE (YaogcWindow, yaogc_window, GTK_TYPE_WINDOW)
     
     
    static gboolean delete_event_handler (GtkWidget *widget, GdkEvent *event, gpointer data);
     
    static void response_event_handler (GtkDialog *dialog, gint response_id, gpointer data);
     
    static void file_quit_cb (GtkAction *action, YaogcWindow *window);
     
    static void edit_preferences_cb (GtkAction *action, YaogcWindow *window);
     
    static void help_about_cb (GtkAction *action, YaogcWindow *window);
     
    static void yaogc_window_realize (GtkWidget *widget);
     
    static void yaogc_window_size_request (GtkWidget *widget, GtkRequisition *requisition);
     
    static void yaogc_window_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
     
    static void yaogc_window_dispose (GObject *object);
     
    static void yaogc_window_finalize (GObject *object);
     
    #ifdef __YAOGC_ENABLE_DEBUG___
    static void
    yaogc_window_size_request_cb (GtkWidget *widget, GtkRequisition *requisition)
    {
        g_print ("\n[window %p] size-request result %d : %d\n",
                 (gpointer)widget, requisition->width, requisition->height);
    }
     
    static void
    yaogc_window_size_allocate_cb (GtkWidget *widget, GtkAllocation *allocation)
    {
        g_print ("\n[window %p] size-alloc result %d : %d at (%d, %d)\n",
                 (gpointer)widget,
                 allocation->width, allocation->height,
                 allocation->x, allocation->y);
    }
    #endif
     
     
    static void
    yaogc_window_init (YaogcWindow *window)
    {
        GtkActionEntry menu_entries[] = {
            /* Toplevel */
            {"File", NULL, "_File", NULL, NULL, NULL},
            {"Edit", NULL, "_Edit", NULL, NULL, NULL},
            {"Help", NULL, "_Help", NULL, NULL, NULL},
     
            /* File menu */
            {"FileQuit", GTK_STOCK_QUIT, "_Quit", "<shift><control>Q", NULL, G_CALLBACK(file_quit_cb)},
            /* Edit menu */
            {"EditPreferences", GTK_STOCK_PREFERENCES, "_Preferences", "<shift><control>P", NULL, G_CALLBACK(edit_preferences_cb)},
            /* Help menu */
            {"HelpAbout", GTK_STOCK_ABOUT, "_About Yaogc", "<shift><control>A", NULL, G_CALLBACK(help_about_cb)}
        };
        YaogcWindowPrivate *priv;
        GtkWidget *main_vbox;
        GtkUIManager *manager;
        GtkActionGroup *action_group;
        GError *error;
     
        /* Getting private members */
        priv = window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, YAOGC_TYPE_WINDOW, YaogcWindowPrivate);
     
    #ifdef __YAOGC_ENABLE_DEBUG___
        g_signal_connect_after (window, "size-request", G_CALLBACK(yaogc_window_size_request_cb), NULL);
        g_signal_connect_after (window, "size-allocate", G_CALLBACK(yaogc_window_size_allocate_cb), NULL);
    #endif
     
        /* Setting window geometry */
        gtk_window_set_default_size (GTK_WINDOW(window), 640, 480);
     
        /* Creating the main YaogcWindow's box */
        main_vbox = gtk_vbox_new (FALSE, 0);
        gtk_container_add (GTK_CONTAINER(window), main_vbox);
        gtk_widget_show (main_vbox);
     
     
        /* Creating the work area */
        window->work_area = gtk_vbox_new (FALSE, 0);
        gtk_box_pack_end (GTK_BOX(main_vbox), window->work_area, TRUE, TRUE, 0);
     
        /* Creating UI manager */
        manager = priv->manager = gtk_ui_manager_new ();
     
        /* Create the actions */
        action_group = priv->action_group = gtk_action_group_new ("Main");
        gtk_action_group_add_actions (action_group, menu_entries, G_N_ELEMENTS(menu_entries), window);
     
        /* Insert action group to UI manager */
        gtk_ui_manager_insert_action_group (manager, action_group, 0);
     
        /* Load the UI */
        error = NULL;
        gtk_ui_manager_add_ui_from_file (manager, __YAOGC_WINDOW_XML___, &error);
     
        if (error) {
            g_printerr ("Failed to load UI: %s\n", error->message);
            g_error_free (error);
        }
     
        /* Adding menubar */
        priv->menu_bar = gtk_ui_manager_get_widget (manager, "/menubar");
        gtk_box_pack_start (GTK_BOX(main_vbox), priv->menu_bar, FALSE, FALSE, 0);
        gtk_widget_show (priv->menu_bar);
     
        /* Create quit dialog */
        priv->dialog_quit = gtk_message_dialog_new (GTK_WINDOW(window),
                                                    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                                    GTK_MESSAGE_QUESTION,
                                                    GTK_BUTTONS_YES_NO,
                                                    "Voulez vous vraiment quitter\nl'application ?");
        g_signal_connect (G_OBJECT(window), "delete-event", G_CALLBACK(delete_event_handler), NULL);
        g_signal_connect (G_OBJECT(priv->dialog_quit), "response", G_CALLBACK(response_event_handler), NULL);
     
     
    }
     
    static void
    yaogc_window_class_init (YaogcWindowClass *window_klass)
    {
        GObjectClass *object_class;
     
        object_class = G_OBJECT_CLASS(window_klass);
     
        object_class->dispose = yaogc_window_dispose;
        object_class->finalize = yaogc_window_finalize;
     
        g_type_class_add_private (object_class, sizeof(YaogcWindowPrivate));
    }
     
    static gboolean
    delete_event_handler (GtkWidget *widget, GdkEvent *event, gpointer data)
    {
        YaogcWindow *window;
        window = YAOGC_WINDOW (widget);
        gtk_widget_show_all (window->priv->dialog_quit);
     
        return TRUE;
    }
     
    static void
    response_event_handler (GtkDialog *dialog, gint response_id, gpointer data)
    {
        gtk_widget_hide_all (GTK_WIDGET(dialog));
        if (response_id == GTK_RESPONSE_YES
            || response_id == GTK_RESPONSE_NONE)
            gtk_main_quit ();
    }
     
    static void
    file_quit_cb (GtkAction *action, YaogcWindow *window)
    {
        g_assert (YAOGC_IS_WINDOW(window));
        gtk_widget_show_all (window->priv->dialog_quit);
    }
     
    static void
    edit_preferences_cb (GtkAction *action, YaogcWindow *window)
    {
     
    }
     
    static void
    help_about_cb (GtkAction *action, YaogcWindow *window)
    {
     
    }
     
    static
    void yaogc_window_dispose (GObject *object)
    {
     
        G_OBJECT_CLASS (yaogc_window_parent_class)->dispose (object);
    }
     
    static
    void yaogc_window_finalize (GObject *object)
    {
        YaogcWindow *window = YAOGC_WINDOW (object);
     
        if (window->priv->manager) {
            g_object_unref (window->priv->manager);
            window->priv->manager = NULL;
        }
     
        G_OBJECT_CLASS (yaogc_window_parent_class)->finalize (object);
    }
     
    GtkWidget*
    yaogc_window_new (void)
    {
        YaogcWindow *window;
        window = YAOGC_WINDOW (g_object_new(YAOGC_TYPE_WINDOW, NULL));
        return GTK_WIDGET(window);
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <!-- yaogc-window.xml -->
     
    <ui>
        <menubar>
            <menu action="File">
                <menuitem action="FileQuit"/>
            </menu>
            <menu action="Edit">
                <menuitem action="EditPreferences"/>
            </menu>
            <menu action="Help">
                <menuitem action="HelpAbout"/>
            </menu>
        </menubar>
    </ui>

  6. #6
    Membre confirmé Avatar de Gamall
    Profil pro
    Étudiant ENSEA
    Inscrit en
    Août 2009
    Messages
    252
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant ENSEA

    Informations forums :
    Inscription : Août 2009
    Messages : 252
    Par défaut
    Après avoir longtemps erré dans les ténèbres au cœur des signaux Gtk+, j'ai enfin trouvé l'origine du mal qui frappe mon programme.

    Celui ci se terrait au abords de ma fonction
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    static gboolean
    delete_event_handler (GtkWidget *widget, GdkEvent *event, gpointer data)
    {
        YaogcWindow *window;
        window = YAOGC_WINDOW (widget);
        gtk_widget_show_all (window->priv->dialog_quit);
     
        return TRUE;
    }
    En effet, comme je renvoie TRUE, la propagation de l'évènement est stoppé, avant que la fonction interne a Gtk ne soit appelée. Or cette fonction a pour but de détruire la fenêtre et son contenu.
    Il faut donc détruire la fenêtre avant de quitter la boucle évènementielle.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Soucis de destruction de widget (NoteBook)
    Par Kintoki dans le forum GTK+ avec C & C++
    Réponses: 0
    Dernier message: 03/01/2015, 13h45
  2. Réponses: 1
    Dernier message: 23/01/2013, 10h52
  3. Problème de destruction de widget
    Par tinram dans le forum GTK+ avec C & C++
    Réponses: 2
    Dernier message: 19/09/2008, 17h52
  4. Affichage de widget après destruction
    Par Zeunknown dans le forum GTK+ avec C & C++
    Réponses: 1
    Dernier message: 31/07/2007, 20h09
  5. Réponses: 7
    Dernier message: 18/04/2003, 10h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo