hugopl / gi-crystal

Tool to generate Crystal bindings for gobject-based libraries (i.e. GTK)
BSD 3-Clause "New" or "Revised" License
45 stars 3 forks source link

Wrong pointer is passed on nillable structs parameters. #162

Open hugopl opened 1 month ago

hugopl commented 1 month ago

Generated bindings for Gtk::Popover#pointing_to= is:

    def pointing_to=(rect : Gdk::Rectangle?) : Nil
      # gtk_popover_set_pointing_to: (Method | Setter)
      # @rect: (nullable)
      # Returns: (transfer none)

      # Generator::NullableArrayPlan
      rect = if rect.nil?
               Pointer(Void).null
             else
               rect.to_unsafe
             end

      # C call
      LibGtk.gtk_popover_set_pointing_to(to_unsafe, rect)

      # Return value handling
    end

This compiles but doesn't work, type of rect in else still a union, the problem is caused by the reuse of the rect variable, if I change it to something else it works.

Monkey patch to fix that:

  class Popover
    def pointing_to=(rect : Gdk::Rectangle?) : Nil
      rect_ptr = if rect.nil?
                   Pointer(Void).null
                 else
                   rect.to_unsafe
                 end
      LibGtk.gtk_popover_set_pointing_to(to_unsafe, rect_ptr)
    end
  end