Search code examples
memory-managementglibgobject

When to free a GObject?


When to unref a GObject and when to free it?
The GObject tutorial states that a GObject should be unrefed and freed, but shouldn't a GObject actually free itself if and only if its refcount drops to 0?


Solution

  • That tutorial describes how and when a custom-made GObject class should free its internal data. Specifically, the difference between the dispose method and the finalize method.

    When you create a GObject, you should only unref it and never free it, or you'll bypass the reference counting system altogether and maybe cause a crash somewhere else.