2026-08-08 by Roman Yepishev.
If you followed GObject 2.0 tutorial, you've seen the following snippet:
/*
* Type declaration.
*/
#define VIEWER_TYPE_FILE viewer_file_get_type()
G_DECLARE_FINAL_TYPE (ViewerFile, viewer_file, VIEWER, FILE, GObject)
/*
* Method definitions.
*/
ViewerFile *viewer_file_new (void);
G_END_DECLS
Later, ViewerFile *viewer_file_new (void); is never referenced, and it is not
immediately clear what should go there.
Do I need to implement the "new" method myself when writing GObject classes in C? - Stack Overflow was asking the same question. After digging more I found an authoritative source at the deprecated Gnome Wiki ( HowDoI/SubclassGObject):
MyAppWindow * my_app_window_new (void) { return g_object_new (MY_APP_TYPE_WINDOW, NULL); }...
As a general rule,
_new()functions should only callg_object_new()with the passed in arguments as properties. If your class has extra code in_new()then it will be difficult (or impossible) to subclass or to use from language bindings.
So the correct code for viewer_file_new is
ViewerFile *
viewer_file_new (void)
{
return g_object_new (VIEWER_TYPE_FILE, NULL);
}
And nothing else. The NULL in this case signifies the end of properties, none in this case.
If you do need properties, you need to specify them:
ViewerFile *
viewer_file_new (void, SomeClass1 *property1_value, SomeClass2 *property2_value)
{
return g_object_new (VIEWER_TYPE_FILE, "property1", property1_value, "property2", property2_value, NULL);
}
