
"Simon Marlow"
No, unfortunately not. You have foo's finalizer which refers to bar via a touchForeignPtr. If both foo and bar are unreachable (references from finalizers don't count), then both foo and bar's finalizers will be started together, and may run in any order.
I didn't realize the "references from finalizers don't count" rule. What would happen if the finalizer of foo would resurrect bar after bar's finalizer has been run?
So touchForeignPtr does only one thing: it expresses the precise relationship "bar is alive if foo is alive". If both are not alive, then both finalizers can run, in any order.
So reference counting the objects is the solution?
I realise this is very subtle. By all means suggest improvements to the docs.
Mentioning that references from finalizers don't count could help someone not to repeat my mistakes. - Einar Karttunen