Re: finalization issues (?)



An, 2002-11-12 03:09, Jacob Perkins rašė:
I have my own GObject called SeahorseWidget, which contains a GladeXML,
plus some extra data.  I've hooked up the finalize function to free the
xml, then sets the xml to null.  To create a new dialog, I create a new
SeahorseWidget, which connect generic signals, such as when the cancel
button is pressed.  When the 'closed' signal calls g_object_unref on the
SeahorseWidget, which then causes finalize to be called.
The problem is that some dialogs can only have one instance at a time. 
I keep a static SeahorseWidget pointer in the dialog's source file.  To
check if an instance already exists, I check if the pointer is null, or
if pointer->xml is null.  If this does not fail, the I call
gtk_window_present(glade_xml_get_widget (swidget->xml, swidget->name)),
which gets the main window.  Unfortunately, the instance test does not
always fail when it should and I get segfaults from
glade_xml_get_widget.

Which is expected behavior. When widget is destroyed, swidget points to
invalid memory address, and you cannot check if swidget->xml is NULL. it
can be anything.

However, for such cases a special method was added to Glib 2:
g_object_add_weak_pointer. When object is finalized, it sets that
pointer to NULL. It means that a pointer is valid only when an object is
alive. So, make it:

void
seahorse_generate_show (SeahorseContext *sctx)
{       
        if (swidget != NULL) {
                gtk_window_present (GTK_WINDOW (glade_xml_get_widget (swidget->xml, swidget->name)));
                return;
        }
        
        swidget = seahorse_widget_new ("generate", sctx);

        g_object_add_weak_pointer (G_OBJECT (swidget), &swidget);

-- 
Gediminas



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