gir.core icon indicating copy to clipboard operation
gir.core copied to clipboard

Methods returning records never return null

Open hol430 opened this issue 2 years ago • 0 comments

If a native method's return type is a record but can be null, the public API should return null if the native code returns NULL. Currently the public API will never return null in this case; instead it will create and return an object with a NULL handle.

This was observed in Gtk.EventController.GetEvent(), which returns a Gdk.Event?.

Attached is a quick example.

(Move the mouse over the window and then off the window to trigger.)

using System;
using Gtk;

var application = Application.New("org.gir.core", Gio.ApplicationFlags.FlagsNone);
application.OnActivate += (sender, args) =>
{
    var window = ApplicationWindow.New((Application) sender);
    window.Title = "Gtk4 Window";
    var controller = EventControllerMotion.New();
    controller.OnLeave += OnLeave;
    window.AddController(controller);
    window.SetDefaultSize(300, 300);
    window.Show();
};

void OnLeave(EventControllerMotion sender, EventArgs args)
{
	Gdk.Event? @event = sender.GetCurrentEvent();
    if (@event == null)
        Console.WriteLine("event is null (this is fine)");
    else if (@event.Handle.Equals(IntPtr.Zero))
        Console.WriteLine($"event is not null but handle is NULL (this shouldn't happen)");
    else
        Console.WriteLine($"event is not null (0x{@event.Handle:x}) (this is fine)");
}

return application.Run();

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="GirCore.Gtk-4.0" Version="0.3.0" />
  </ItemGroup>

</Project>

hol430 avatar May 12 '23 05:05 hol430