Learning GObject basics

GLib and GObject enable object oriented programming in pure C. I will need a GObject later to build a D-Bus interface, so I wanted to write a simple class to learn how to use it. After reading “Introduction to GObject” by Amos Brocco and the tutorial part of the GObject Reference Manual, I started writing my own class. I could take a large part of the basic code from the examples in the reference manual, but a few things were somewhat confusing:

  • Which functions do I have to implement?
  • Where should I define what struct?
  • Do I need to allocate the private memory?
  • Should the object struct contain a pointer to the private memory?

Here are the answers that I found:

Necessary functions

Four functions are the absolute minimum: init, class_init, dispose and finalize. For a class named example, you would need the following declarations in the header:

static void example_init(Example *self);
static void example_class_init(ExampleClass *klass);
static void example_dispose(GObject *gobject);
static void example_finalize(GObject *gobject);

The argument to class_init should be called klass to avoid confusion with the C++ class keyword.


The object und class structs have to be declared and defined in the header (example). You can put the private data struct declaration into the header or source, the definition should definitely be in the source file. I would recommend putting the typedef into the header, because it will allow you to add a pointer to the private memory into the object struct. Put something like this into the header:

typedef struct _Example Example;
typedef struct _ExampleClass ExampleClass;
typedef struct _ExamplePrivate ExamplePrivate;

struct _Example {
	GObject parent_instance;
	/* instance members */
	ExamplePrivate *priv;

struct _ExampleClass {
	GObjectClass parent_class;
	/* class members */

The definition of struct _ExamplePrivate goes into the source file. The contents of the private structure is entirely your decision.

Allocating and accessing private memory

Register the private memory with the object system using g_type_class_add_private() in your class’ class_init() function. It will be allocated automatically when you use G_TYPE_INSTANCE_GET_PRIVATE for the first time, and freed automatically as well. However, you have to take care of any dynamically allocated memory in the private data. To quote the manual:

“You don’t need to free or allocate the private structure, only the objects or pointers that it may contain.”

If you use the source layout recommended in the manual, you will get a useful macro called something like EXAMPLE_GET_PRIVATE which returns a pointer to the private memory. Using a *priv pointer is not necessary (you could use GET_PRIVATE each time you need it), but it is a good idea to improve performance (the macro does a few function calls).

You can see how to register and get private memory in the example source file, look at the maman_bar_class_init() and maman_bar_init() functions and the MAMAN_BAR_GET_PRIVATE macro.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: