Fix memory leaks and nondeterministic IO behavior.

Also removes obsolete example code, which relied on an old version of
GLFW. The example code also failed to make apparent the memory leaks and
nondeterministic IO problems (due to unsafePerformIO being used), so it
should be rewritten regardless.

Many issues still remain.