libui.cr icon indicating copy to clipboard operation
libui.cr copied to clipboard

Trouble when creating an object instance of UI::Window* inside constructor initialize

Open felipetesc opened this issue 7 years ago • 1 comments

Hello. Patience with me, I'm new to Crystal, as we all are, I guess. Excuses apart, I'm trying to create a UI::Window* object inside the constructor and there seems to be a problem. Best if I show you the code, it will be explained by itself:

require "../lib/libui/src/libui/libui.cr"
require "../lib/libui/src/libui/cui.cr"

module Prismatic
  # test
  # ControlGallery.new

  class CryWindow
    @title : String
    @width : Int32
    @height : Int32
    @@main_window : UI::Window* | Nil

    def initialize(title : String, width : Int32, height : Int32)
      @title = title == nil ? "Application" : title
      @width = width == nil ? 800 : width
      @height = height == nil ? 600 : height
      @@main_window = UI.new_window @title, @width, @height, 1
    end

    def show
      # initialize libui
      init_libui
      # # CREATING MAIN WINDOW
      # @@main_window = UI.new_window @title, @width, @height, 1

      mainwin = @@main_window.not_nil!

      UI.window_set_margined @@main_window, 10

      default_window_operations

      createlayout @@main_window

      UI.control_show ui_control(mainwin)

      UI.main

      UI.uninit
    end

    private def init_libui
      # INITIALIZING LIBUI

      ui_obj_ref = UI::InitOptions.new

      errors = UI.init pointerof(ui_obj_ref)

      if !ui_nil?(errors)
        puts "error initializing ui: #{errors}"
        exit 1
      end
    end

    private def default_window_operations
      UI.window_on_closing @@main_window, default_close_operation, nil
    end

    private def default_close_operation
      ->(w : UI::Window*, data : Void*) {
        UI.control_destroy ui_control(@@main_window.not_nil!)
        UI.quit
        0
      }
    end

    def createlayout(mainwin)
      box = UI.new_vertical_box

      UI.box_set_padded box, 1

      UI.window_set_child mainwin, ui_control(box)

      hbox = UI.new_horizontal_box

      UI.box_set_padded hbox, 1

      UI.box_append box, ui_control(hbox), 1
    end
  end

  cryWindow = CryWindow.new("Prismatic", 800, 600)
  cryWindow.show
end

The output below shows the errors

(process:6099): GLib-CRITICAL **: g_ptr_array_add: assertion 'rarray' failed
Invalid memory access (signal 11) at address 0x0
[0x5654c172fb15] *CallStack::print_backtrace:Int32 +117
[0x5654c1724dcd] __crystal_sigfault_handler +61
[0x7f130453a150] ???
[0x7f13034f7659] ???
[0x7f130338f748] ???
[0x7f13033a5273] ???
[0x7f1303390aac] ???
[0x7f13033a51ac] ???
[0x7f13033a5205] ???
[0x7f1303391422] ???
[0x7f1302579738] g_type_create_instance +424
[0x7f130255a5e8] ???
[0x7f130255bd85] g_object_new_with_properties +757
[0x7f130255c801] g_object_new +193
[0x7f13033ad58a] ???
[0x7f130358bab8] ???
[0x7f1302579738] g_type_create_instance +424
[0x7f130255a5e8] ???
[0x7f130255c460] g_object_new_valist +976
[0x7f130255c7d9] g_object_new +153
[0x7f130476300c] uiNewWindow +237
[0x5654c1768024] *Prismatic::CryWindow#initialize<String, Int32, Int32>:Pointer(UI::Window) +196
[0x5654c1767f41] *Prismatic::CryWindow::new<String, Int32, Int32>:Prismatic::CryWindow +97
[0x5654c171556b] ???
[0x5654c1724cc9] main +41
[0x7f1303b451c1] __libc_start_main +241
[0x5654c1714e7a] _start +42
[0x0] ???
[Finished in 0.8s with exit code 11]
[shell_cmd: crystal "/home/ftorresini/Projects/Prismatic/src/Prismatic.cr"]
[dir: /home/ftorresini/Projects/Prismatic/src]
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]

The code below, works:

require "../lib/libui/src/libui/libui.cr"
require "../lib/libui/src/libui/cui.cr"

module Prismatic

  class CryWindow
    @title : String 
    @width : Int32
    @height : Int32
    @@main_window : UI::Window* | Nil

    def initialize(title : String, width : Int32, height : Int32)
      @title = title == nil ? "Application" : title
      @width = width == nil ? 800 : width
      @height = height == nil ? 600 : height
    end

    def show
      # initialize libui
      init_libui
      # # CREATING MAIN WINDOW
      @@main_window = UI.new_window @title, @width, @height, 1

      mainwin = @@main_window.not_nil!

      UI.window_set_margined @@main_window, 10

      default_window_operations

      @window_initialized = true

      createlayout @@main_window

      UI.control_show ui_control(mainwin)

      UI.main

      UI.uninit
    end

    private def init_libui
      # INITIALIZING LIBUI

      ui_obj_ref = UI::InitOptions.new

      errors = UI.init pointerof(ui_obj_ref)

      if !ui_nil?(errors)
        puts "error initializing ui: #{errors}"
        exit 1
      end
    end

    private def default_window_operations
      UI.window_on_closing @@main_window, default_close_operation, nil
    end

    private def default_close_operation
      ->(w : UI::Window*, data : Void*) {
        UI.control_destroy ui_control(@@main_window.not_nil!)
        UI.quit
        0
      }
    end

    def createlayout(mainwin)
      if @window_initialized == true
        box = UI.new_vertical_box

        UI.box_set_padded box, 1

        UI.window_set_child mainwin, ui_control(box)

        hbox = UI.new_horizontal_box

        UI.box_set_padded hbox, 1

        UI.box_append box, ui_control(hbox), 1
      else
        raise Exception.new("The method createlayout should be called before the method show")
      end
    end
  end

  cryWindow = CryWindow.new("Prismatic", 800, 600)
  cryWindow.show
end 

So, what can I do? I am a little lost. can anyone help? Is it a Crystal issue ?

Thank You.

felipetesc avatar Dec 11 '17 21:12 felipetesc

PS: I only want to create a single window instance and use it all over the class.

felipetesc avatar Dec 11 '17 21:12 felipetesc