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

Crash when Area component callback accesses instance variable #3

Open
existXFx opened this issue Nov 29, 2021 · 2 comments
Open

Crash when Area component callback accesses instance variable #3

existXFx opened this issue Nov 29, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@existXFx
Copy link

A class inherits Iu::Components::Area and implements on_* callback, will crash program when its instance access instance variables:

class MyArea < Iu::Components::Area
    def initialize(width = nil, height = width)
      @blah = "blah"
      super(width, height)
    end

    def on_draw(params : UI::AreaDrawParams)
       puts @blah
    end

    def on_mouse_event(mouse_event : UI::AreaMouseEvent);end
    def on_mouse_crossed(left : Bool);end
    def on_drag_broken;end
    def on_key_event(key_event : UI::AreaKeyEvent);end
end

I'm not familiar with Crystal. I think the reason is that the callback function passed to the libui C library forms a closure. The Iu shard wraps libui, a new member "data" is added to the AreaHandler structure to store pointers to an area objects. This allows libui to call back the on_* method implemented by the MyArea class, I'm not sure if this is the scenario described in Crystal document. After testing, this bug can be solved in this way:

@@on_mouse_event_cb = Proc(UI::AreaHandler*, UI::Area*, UI::AreaMouseEvent*, Nil).new { |h, a, e|
        # h.value.data.as(Area*).value.on_mouse_event(e.value).as(Nil)
        ::Box(Area).unbox(h.value.data).on_mouse_event(e.value).as(Nil)
}
# @handler.data = self.as(Void*)
@handler.data = ::Box.box(self)
@grkek grkek added the enhancement New feature or request label Dec 30, 2021
@grkek
Copy link
Owner

grkek commented Dec 30, 2021

Make a PR for it mate!

@videotoaster
Copy link
Contributor

Went ahead and opened the PR, I applied this fix to each of the callbacks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants