Re: g_object_unref



On Fri, 2003-08-01 at 19:50, Ivo Andrejco wrote:
Hi,
could someone , please tell me how to destroy mu custom object. For
example, if I create MyObject as subclass of GObject. How do I destroy
it. Will call to g_object_unref release any memory dynamically allocated
by my object. I would like to know how to safely destroy my MyObject
without memory leaks. Thanks
Ivo

You should unref any objects you hold references to in the "dispose"
vtable function, free any memory you have allocated by using the
"finalize" vtable function.

Example:

static gpointer parent_class = NULL;

static void
my_object_finalize (GObject *object)
{
    MyObject *myobj = MY_OBJECT (object);

    g_free (myobj->some_string);
    // etc.

    if (G_OBJECT_CLASS (parent_class)->finalize != NULL)
        (*G_OBJECT_CLASS (parent_class)->finalize) (object);
}


static void
my_object_dispose (GObject *object)
{
    MyObject *myobj = MY_OBJECT (object);

    g_object_unref (myobj->some_other_object);

    if (G_OBJECT_CLASS (parent_class)->dispose != NULL)
        (*G_OBJECT_CLASS (parent_class)->dispose) (object);
}

static void
my_object_class_init (MyObjectClass *class)
{
    GObjectClass *object_class = G_OBJECT_CLASS (class);

    parent_class = g_type_class_peek_parent (class);

    object_class->finalize = my_object_finalize;
    object_class->dispose = my_object_dispose;

    // Anything else you need to do in class_init.
}

-- 
Peace,

    Jim Cape
    http://ignore-your.tv

    "It is literally true that, like Christianity, Socialism
     has conquered the world by defeating itself."
        -- Alexander Berkman, ABC of Anarchism

Attachment: signature.asc
Description: This is a digitally signed message part



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