Table of Contents

GirCore1002

  • Title: GObject subclass constructor without parameters must use partial 'Initialize' method
  • Message: GObject subclass constructor without parameters must use 'partial void Initialize()' method instead of a constructor

Using a custom constructor instead of the Initialize() method does not initialize the instance in all cases. There are generated constructors which can get called if a new instance is created by native code. Those constructors call the Initialize() method.

Invalid code:

[Subclass<Gtk.Widget>]
public partial class MyWidget
{
    private readonly GObject.Object _data;

    public MyWidget()
    {
        _data = GObject.Object.NewWithProperties([]);
        ...
    }
}

Solution:

[Subclass<Gtk.Widget>]
public partial class MyWidget
{
    private GObject.Object _data;

    [MemberNotNull(nameof(_data))]
    partial void Initialize()
    {
        _data = GObject.Object.NewWithProperties([]);
        ...
    }
}