Use stream object safely (fixes win32 shutdown crash)