Chris00 / ocaml-cairo

Binding to Cairo, a 2D Vector Graphics Library.
GNU Lesser General Public License v3.0
54 stars 8 forks source link

Finalization function for cairo context needed #10

Closed wildptr closed 3 years ago

wildptr commented 6 years ago

I'm drawing to GTK drawing areas using cairo. All drawing is done via a cairo context created within the expose event handler. However, I have run into 2 problems (which seem to be Mac-specific):

(On Windows and Linux everything looks fine.)

However I have found a workaround: perform GC manually in order to finalize the cairo object when the expose event handler is done with it. However, this is inefficient, since all that's needed is calling the finalizer of the cairo object, which is to my knowledge not possible in OCaml. So I think it might be necessary to add a finalization function that allows the user to explicitly destroy the cairo object.

If the cairo context used for drawing is created outside the event handler, the program does not crash, but exposed regions don't get updated.

I'm fairly new to GTK and cairo so please correct me if I get something wrong.

Chris00 commented 6 years ago

If the cairo context used for drawing is created outside the event handler, the program does not crash, but exposed regions don't get updated.

This is normal, see the note “due to double-buffering, Cairo contexts created in a GTK+ expose event handler cannot be cached and reused between different expose events”.

Chris00 commented 6 years ago

So I think it might be necessary to add a finalization function that allows the user to explicitly destroy the cairo object.

I do not see the problem in keeping the cairo context in memory a while longer until it gets garbage collected. I'd like to make sure to understand the problem first.

Chris00 commented 6 years ago

Does your problem happens with the gtk_demo in the sources?

Chris00 commented 6 years ago

Could you try to recompile the current version and let me know?

@bobot I will rebase again when the fix is confirmed to work (sorry).

wildptr commented 6 years ago

Yes, this happens with gtk_demo on Mac (not yet tried on other OS's). The exposed region of the window does not get updated when window is enlarged.

Chris00 commented 6 years ago

May you try to add some printing (and flushing!) in the #expose callback to make sure it is indeed triggered?

Chris00 commented 6 years ago

I have added some printing to the test. You can do

dune build @tests-gtk --force

(but then the printing will eb delayed by dune) or, after dune build @tests-gtk run _build/default/tests-gtk/alloc.exe (to see the printing immediately).

Chris00 commented 5 years ago

@wildptr Any news?

wildptr commented 5 years ago

Sorry for being unresponsive for a long time. I missed notifications for your comments :( I'm busy with work these days and I will come back with this issue some time later.

Chris00 commented 3 years ago

Please reopen if with more info if it is not fixed.