Custom Widget: How to announce a destructor?

,

Question

Can someone tell me how I can tell GObject, which function my destructor function is?

Code

For reference, I post example skeleton code for a widget.

header

#ifndef __MY_WIDGET_H__
#define __MY_WIDGET_H__

#include <gtk/gtk.h>

G_BEGIN_DECLS

#define TYPE_MY_WIDGET my_widget_get_type ()


// note: member vars of derivable classes go into MyWidgetPrivate (see c file)

G_DECLARE_DERIVABLE_TYPE(MyWidget, my_widget, MY, WIDGET, GtkButton)
struct _MyWidgetClass
{
  GtkButtonClass parent_class;
};

GtkWidget* my_widget_new (void);

G_END_DECLS

#endif /* include-protector */

c-file

#include "mywidget.h"

typedef struct _MyWidgetPrivate MyWidgetPrivate;
struct _MyWidgetPrivate
{
  char dummy; // we need at least one elem within our class
};
G_DEFINE_TYPE_WITH_PRIVATE(MyWidget, my_widget,  GTK_TYPE_BUTTON)


// @brief: class constructor
static void 
my_widget_class_init (MyWidgetClass* klass)
{
  // override class methods from GtkButton:
  // GtkButtonClass * parent = GTK_BUTTON_CLASS(klass);  
}


// object constructor
static void 
my_widget_init (MyWidget* self)
{  
  // init variables

  // init private variables:
  // MyWidgetPrivate * priv = my_widget_get_instance_private(self);
}

// object destructor: How do I announce it?
stattic void
my_widget_uninit (MyWidget *self)
{

}



GtkWidget*
my_widget_new ()
{
  GtkWidget * self = g_object_new (my_widget_get_type(), NULL);   
  // stuff that doesn't work within init can be done here  
  return self;
}

There are a few ways destruction is handled:

  1. The dispose vfunc on GObject, which should drop all references to other GObjects (and maybe reference counted boxed types too). This is used to break reference cycles.
  2. The finalize vfunc on GObject, which should free all data private to the GObject instance.
  3. The destroy signal on GtkWidget, which in my experience is only really useful from language bindings.

If you want to use dispose and finalize, you would need to override them from your class_init. You may not need both, depending on your object you may only need to use one. Also see some examples from the Gtk docs and GObject docs on what the code should look like.

Taking a quick look, I understand

  • finalize is the general way to go - For general simple classes.
  • dispose is rather for more specific purposes and I will need to think about it, if I find finalize is not serving my needs anymore.

Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.