Hobby-hacking Eric


no start twice

Yippee! I found it! And this tiny moment of joy has made the whole miserable sniffly headachy weekend (got a cold) worth it.

I ferreted out the problem by making a small wxhaskell demonstrator and trying to figure out what Control.Exception.bracket is all about. As far as I can tell, it doesn't look like more than just a >>= c >>= b, so we can forget about it. The problem is more to do with the start function. Here's what my tester boiled down to:
main = createGui ""

createGui txt =
do putStrLnAndFlush $ "opening " ++ txt
start $ gui txt
putStrLnAndFlush $ "closing " ++ txt

gui txt =
do f <- frame [text := "Howdy " ++ txt ]
closeBt <- button f [ text := "Close"
, on command := close f ]
spawnBt <- button f [ text := "Spawn"
, on command := createGui $ txt ++ "."
set f [ layout := margin 5 $ column 2 [ widget closeBt
, widget spawnBt ] ]
putStrLnAndFlush $ "gui created " ++ txt

putStrLnAndFlush x = putStrLn x >> hFlush stdout

This demonstrator allows us to replicate the issue I was having with wxDarcs. That is, you click on the spawn button to create a second window, and if you close the second window, it does not seem to come back. Exactly like yesterday, if you close the first window, you get a bus error. All you need to do to fix this is to realise that start indeed performs some kind of voodoo, and this voodoo is something you may only perform once. It was one of my theories, but I had written some code into Record.lhs to not call start, but it didn't seem to work. Maybe I had just confused myself or something. So, the simple fix is just to change the code so it looks more like this:
main = start $ createGui ""

createGui txt =
do putStrLnAndFlush $ "opening " ++ txt
gui txt
putStrLnAndFlush $ "closing " ++ txt

This way, when you call that createGui function from within the gui, you're not calling that start function a second time. Woohoo! Then maybe we're not so far from getting wxDarcs out the door so that other people can hack on it and so that I can start working on other stuff.

No comments:

Blog Archive