craff / gles3

ocaml bindings for gles3
GNU Lesser General Public License v3.0
4 stars 0 forks source link

Segmentation fault when motion event #5

Closed rlepigre closed 3 years ago

rlepigre commented 3 years ago

After fixing the handling of motion events (see 1c39e1af8cda67b5ffbe6180a862a48be99aa616), I tried using the following code (which works fine) in the maze example:

let _ =
  let handle_motion ~state ~x ~y =
    Printf.printf "handle_motion ~state:%i ~x:%i ~y:%i\n%!" state x y
  in
  Egl.set_motion_notify_callback handle_motion

I then wanted to change it to obtain position differences:

let _ =
  let last_x = ref 0 in
  let last_y = ref 0 in
  let valid = ref false in
  let handle_motion ~state ~x ~y =
    if !valid then
      let dx = x - !last_x in
      let dy = y - !last_y in
      last_x := x; last_y := y;
      Printf.printf "Position diff: (%i, %i)\n%!" dx dy
    else
      (last_x := x; last_y := y; valid := true)
  in
  Egl.set_motion_notify_callback handle_motion

However, this version of the code segfaults as soon as the mouse enters the window. I tried debugging this, but I could not find the problem quickly. In particular, I don't understand why the keyboard events seem to have no problem with similar code.

Any idea? Is this related to the stuff you recently fixed @craff?

craff commented 3 years ago

Le 21-01-01 16:59:47, Rodolphe Lepigre a écrit :

After fixing the handling of motion events (see 1c39e1a), I tried using the following code (which works fine) in the maze example:

let _ = let handle_motion ~state ~x ~y = Printf.printf "handle_motion ~state:%i ~x:%i ~y:%i\n%!" state x y in Egl.set_motion_notify_callback handle_motion

I then wanted to change it to obtain position differences:

let _ = let last_x = ref 0 in let last_y = ref 0 in let valid = ref false in let handle_motion ~state ~x ~y = if !valid then let dx = x - !last_x in let dy = y - !last_y in last_x := x; last_y := y; Printf.printf "Position diff: (%i, %i)\n%!" dx dy else (last_x := x; last_y := y; valid := true) in Egl.set_motion_notify_callback handle_motion

However, this version of the code segfaults as soon as the mouse enters the window. I tried debugging this, but I could not find the problem quickly. In particular, I don't understand why the keyboard events seem to have no problem with similar code.

Any idea? Is this related to the stuff you recently fixed @craff?

Tested in maze.ml with:

let _ = ( Initialise the main window. ) Egl.initialize !window_width !window_height "maze"; ( Initialise its viewport. ) Gles3.viewport ~x:0 ~y:0 ~w:!window_width ~h:!window_height; ( Setup the reshape callback. ) let reshape ~width ~height = window_width := width; window_height := height; window_ratio := (float width) /. (float height); Gles3.viewport ~x:0 ~y:0 ~w:width ~h:height in Egl.set_reshape_callback reshape; let last_x = ref 0 in let last_y = ref 0 in let valid = ref false in let handle_motion ~state ~x ~y = if !valid then let dx = x - !last_x in let dy = y - !last_y in last_x := x; last_y := y; Printf.printf "Position diff: (%i, %i)\n%!" dx dy else (last_x := x; last_y := y; valid := true) in Egl.set_motion_notify_callback handle_motion

No problem so far ...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.*

rlepigre commented 3 years ago

I pushed the exact code that segfaults for me to the segfault branch. Can you try dune exec maze in it to see if you can reproduce?

rlepigre commented 3 years ago

In fact, the problem only seems to happen with earlier versions of OCaml (e.g. 4.07.1). I just tried with 4.11.1 and the problem is gone.

craff commented 3 years ago

Le 21-01-02 03:48:30, Rodolphe Lepigre a écrit :

In fact, the problem only seems to happen with earlier versions of OCaml (e.g. 4.07.1). I just tried with 4.11.1 and the problem is gone.

In 4.09.0, no pb either.

Using normal global root also fixes the pb. I added in the segfault branch two macros in ml_egl.c redefining global roots in normal roots. Maybe we can apply this macro conditionnally depending on OCaml's version as generational roots are advertised as more efficient ?

Cheers, Christophe

Note: I will be away for internet for a week ...

craff commented 3 years ago

Solved by merging segfault branch (merge b7760af 6a51199)