garetxe on master
Add missing deps (compare)
boxPackStart box widget expand fill padding
widgetDestroy
which appears to trigger the crash.
I'm then destroying the widget with
widgetDestroy
which appears to trigger the crash.
This sounds a bit like you are double freeing. Is it necessary to call widgetDestroy
in your code? Every widget in Gtk is reference counted. In particular if you create a widget and add it to a box then it will have a refcount of 2: Haskell keeps one (which it will drop once the Haskell object is garbage collected) and the container keeps another.
(By the way, when debugging crashes it is useful to run with the following environment vars set:
export G_SLICE="debug-blocks"
export MALLOC_CHECK_=2
export MALLOC_PERTURB_=$(($RANDOM % 255 + 1))
export HASKELL_GI_DEBUG_MEM=1
)
And another question: how would I replicate https://hackage.haskell.org/package/gi-gtk-4.0.5/docs/GI-Gtk-Objects-Scale.html#v:scaleNew with https://hackage.haskell.org/package/haskell-gi-base-0.26.0/docs/Data-GI-Base-Constructible.html#v:new , since it has some additional parameters ?
In general it should be perfectly fine to call scaleNew
, but in this case it might be possible to make it work with new
too. Have you tried setting the orientation
and adjustment
attributes? Something like
scale <- new Gtk.Scale [#orientation := ..., #adjustment := ...]
data HPackBox = HPackBox
instance (IsDescendantOf Box a, IsDescendantOf Widget b,
Constructible b tag, MonadIO m, GObject a, GObject b,
input ~ ((a, Bool, Bool, Word32), (ManagedPtr b -> b, [AttrOp b tag])), output ~ m b) =>
ApplyAB HPackBox input output where
applyAB _ ((box, expand, fill, padding), (ctr, attrs)) = do
widget <- new ctr attrs
boxPackStart box widget expand fill padding
return widget
data HDestroyWidget = HDestroyWidget
instance (IsWidget o) => ApplyAB HDestroyWidget o (IO ()) where
applyAB _ widget = widgetDestroy widget
(Button, [#label := ("Hello World 1"::Text)]) .*.
(Button, [#label := ("Hello World 1"::Text)]) .*.
(Button, [#label := ("Hello World 1"::Text)]) .*.
(Button, [#label := ("Hello World 1"::Text)]) .*.
(Switch, []) .*. -- crashes ?
(Scale, []) .*.
HNil)
So my question is if every newX method can be replicated with new and attributes.
Ah, I see, thanks for the explanation. (Cool approach!) I hesitate to say that all widgets can be created in this way (I cannot think of an exception now, but I'm not sure), but this certainly seems to be the direction of travel.
containerRemove
doesn't cause the crash.
Yes, this is definitely safer. By doing this you are telling the container to drop its reference to the widget, but you are not invalidating it, so the widget is not becoming invalid "behind Haskell's back", so to speak
So my question is if every newX method can be replicated with new and attributes.
Ah, I see, thanks for the explanation. (Cool approach!) I hesitate to say that all widgets can be created in this way (I cannot think of an exception now, but I'm not sure), but this certainly seems to be the direction of travel.
I tried the following and it worked !
adjustment <- new Adjustment [#value := 0.0, #lower := 0.0, #upper := 51, #stepIncrement := 0.1, #pageIncrement := 1.0, #pageSize := 1.0]
scale <- new Scale [ #orientation := OrientationVertical, #adjustment := adjustment]
So that means that every "xNew" method has corresponding attributes that are generated automatically with the same name as the name given to the function "xNew"'s arguments in the documentation ?
I tried the following and it worked !
Great :)
So that means that every "xNew" method has corresponding attributes that are generated automatically with the same name as the name given to the function "xNew"'s arguments in the documentation ?
It is not automatic, unfortunately, the writer of the C code has to make sure that the relevant arguments are exposes as GObject properties. But in practice, in Gtk at least, it seems to almost always be the case, and all the different constructors are just convenience wrappers over g_object_new
, setting the properties. Something like what you see in gtk_button_new_with_label
.
new
with attributes in haskell-gi
.
gtk_message_dialog_new
is doing internally, you can check here.
gtk_message_dialog_new
is not in the bindings is because it accepts a variable number of arguments, and we don't bind such functions.)
Nothing
if no such data exists”nullPtr
if no data exists?
gtk-doc
-> haddock
translator uses a number of heuristics to improve the Haskell side docs, one of which is replacing NULL
by Nothing
. This is almost always the right thing to do, since nullable arguments on the C side are mapped to Maybe
arguments. But this is indeed wrong for objectGetData
.
resolveAttr
, for example by doing the following inside cabal repl
for your project::set -XTypeApplications
:set -XOverloadedLabels
import qualified GI.Gtk as Gtk
import Data.GI.Base.Attributes (resolveAttr)
resolveAttr (undefined :: Gtk.Stack) #transitionType
also things like new Gtk.Box [#orientation := Gtk.OrientationHorizontal] how is new defined? is it a function? if so why isn't it prefixed with something?
new
comes from Data.GI.Base: https://hackage.haskell.org/package/haskell-gi-base/docs/Data-GI-Base.html#v:new