PerlIO_exportFILE() and PerlIO_releaseFILE() should manage the fd
Nicholas Clark [Mon, 19 Mar 2007 15:17:27 +0000 (15:17 +0000)]
reference counts correctly.

p4raw-id: //depot/perl@30633

ext/XS/Typemap/Typemap.xs
perlio.c

index 3e44867..dbb1733 100644 (file)
@@ -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
 
index bba198c..ef07055 100644 (file)
--- 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;
            }