Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods returning records never return null #867

Closed
hol430 opened this issue May 12, 2023 · 1 comment
Closed

Methods returning records never return null #867

hol430 opened this issue May 12, 2023 · 1 comment
Assignees
Milestone

Comments

@hol430
Copy link
Contributor

hol430 commented May 12, 2023

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>
@badcel
Copy link
Member

badcel commented May 7, 2024

Fixed with #1056

@badcel badcel closed this as completed May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants