From 6b54a40376f816c07bf5262ecec28e130b304cb8 Mon Sep 17 00:00:00 2001 From: Nicholas Clark Date: Mon, 19 Mar 2007 15:17:27 +0000 Subject: [PATCH] PerlIO_exportFILE() and PerlIO_releaseFILE() should manage the fd reference counts correctly. p4raw-id: //depot/perl@30633 --- ext/XS/Typemap/Typemap.xs | 3 ++- perlio.c | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/ext/XS/Typemap/Typemap.xs b/ext/XS/Typemap/Typemap.xs index 3e44867..dbb1733 100644 --- a/ext/XS/Typemap/Typemap.xs +++ b/ext/XS/Typemap/Typemap.xs @@ -802,10 +802,11 @@ T_STDIO_close( f ) CODE: /* Get the FILE* */ stream = PerlIO_findFILE( f ); - RETVAL = xsfclose( stream ); /* Release the FILE* from the PerlIO system so that we do not close the file twice */ PerlIO_releaseFILE(f,stream); + /* Must release the file before closing it */ + RETVAL = xsfclose( stream ); OUTPUT: RETVAL diff --git a/perlio.c b/perlio.c index bba198c..ef07055 100644 --- a/perlio.c +++ b/perlio.c @@ -3552,6 +3552,7 @@ PerlIO_exportFILE(PerlIO * f, const char *mode) if ((f2 = PerlIO_push(aTHX_ f, PERLIO_FUNCS_CAST(&PerlIO_stdio), buf, NULL))) { PerlIOStdio *s = PerlIOSelf((f = f2), PerlIOStdio); s->stdio = stdio; + PerlIOUnix_refcnt_inc(fileno(stdio)); /* Link previous lower layers under new one */ *PerlIONext(f) = l; } @@ -3591,6 +3592,9 @@ PerlIO_releaseFILE(PerlIO *p, FILE *f) PerlIOStdio *s = PerlIOSelf(&l, PerlIOStdio); if (s->stdio == f) { dTHX; + const int fd = fileno(f); + if (fd >= 0) + PerlIOUnix_refcnt_dec(fd); PerlIO_pop(aTHX_ p); return; } -- 2.7.4