> Is their a possibility to ensure that they will run, before 
> the program
> terminates.

No, I'm afraid not.  They'll only run when the garbage collector can prove
that they're not reachable from anywhere.

> Eventually. I have to kill some threads, holding other Objects and to
> synchronise with an external database when the object is dead.
> 
> > > If I trigger a gc in hugs the object is garbage collected but not
> > > finalized.
> >
> > Which version of GHC/Hugs are you using exactly?
> >
> 
> GHC: 4.06

There haven't been any bugs fixed in finalization since 4.06, and it seems
to be working fine at the moment.

Weak pointers and finalizers are pretty tricky things, though: it's all too
easy to retain a pointer from a thunk (like a space leak) and prevent the
finalizer from running.  The heap profiler (in the upcoming 4.07) may help
you identify any space leaks, and it will also confirm that the objects you
expect to be finalized are still live in the heap.  What it won't tell you,
is what is hanging on to them.  We need retainer profiling for that, and
it's not implemented in GHC at the moment (NHC has it).

> I'm talking about Weak.finalize and using weak-pointers for 
> checking the
> status, if the object is collected.
> I don't use foreign objects. I would like to kill threads which hold
> subobjects with the aid of finalizers.
> I will attach a short example.
> I used these options for running: +RTS -B -G1 -A5k -RTS
> 
> I hope you will apologize my eventually bad or unusual programming
> style. I
> just learned Haskell for my thesis.

I looked at your example.  You've been a bit unlucky here!  In a nutshell,
here's what happened:

        - the function test was inlined into main
        - 4 was substituted for z
        - port is now a static value, and was therefore
          floated out to the top level
        - top level constructors aren't garbage collected

so 'port' was never found to be garbage, because it was a top-level static
value.  To see finalizers in action, add

{-# NOINLINE test #-}

to the module, to stop test from being inlined.

I think I'll add a warning to mkWeak when you attempt to attach a weak
pointer to a static object.

Cheers,
        Simon

Reply via email to