diff --git a/C/iopreds.c b/C/iopreds.c index 0b2f269cf..644ac9019 100644 --- a/C/iopreds.c +++ b/C/iopreds.c @@ -5371,6 +5371,8 @@ p_format2(void) CloseStream(stream); if (!Yap_unify(tat,ArgOfTerm(1,ARG1))) return FALSE; + } else { + CloseStream(stream); } } else { Yap_c_output_stream = old_c_stream; diff --git a/pl/yio.yap b/pl/yio.yap index e9b1ef17d..14713149c 100644 --- a/pl/yio.yap +++ b/pl/yio.yap @@ -1055,15 +1055,16 @@ current_stream(File, Opts, Stream) :- catch(Goal, Exception, '$handle_exception'(Exception,Stream,SO)), !, set_output(SO), - charsio:peek_mem_write_stream(Stream, L0, Chars). + charsio:peek_mem_write_stream(Stream, L0, Chars), + close(Stream). '$do_output_to_chars'(_Goal, Stream, _L0, _Chars, SO) :- set_output(SO), close(Stream), fail. '$handle_exception'(Exception, Stream, SO) :- + set_output(SO), close(Stream), - current_output(SO), throw(Exception). write_depth(T,L) :- write_depth(T,L,_).