all: close redirected stdout before global destruction

If it's redirected to a file then this is an instance of a buffered file
writer. If it's only collected via global destruction after its
reference count reaches zero then flushing the buffers will call some
debugging option stuff which is invalid at that point.