[macruby-changes] [2124] MacRuby/branches/experimental/io.c

source_changes at macosforge.org source_changes at macosforge.org
Fri Jul 31 19:45:02 PDT 2009


Revision: 2124
          http://trac.macosforge.org/projects/ruby/changeset/2124
Author:   lsansonetti at apple.com
Date:     2009-07-31 19:45:02 -0700 (Fri, 31 Jul 2009)
Log Message:
-----------
keep streams that should never be closed into a static array to avoid them being collected (then later closed since a stream finalizer closes itself...)

Modified Paths:
--------------
    MacRuby/branches/experimental/io.c

Modified: MacRuby/branches/experimental/io.c
===================================================================
--- MacRuby/branches/experimental/io.c	2009-07-31 22:55:27 UTC (rev 2123)
+++ MacRuby/branches/experimental/io.c	2009-08-01 02:45:02 UTC (rev 2124)
@@ -41,6 +41,13 @@
 VALUE rb_deferr;		/* rescue VIM plugin */
 static VALUE orig_stdout, orig_stderr;
 
+// kept_streams is an Array of CFStreams that cannot be released, because
+// their file descriptor would be closed at the very same time. These streams
+// are basically standard IO streams (stdio) but also streams created by 
+// IO#initialize (accepting a given file descriptor).
+// TODO: not thread-safe.
+static VALUE kept_streams;
+
 VALUE rb_output_fs;
 VALUE rb_rs;
 VALUE rb_output_rs;
@@ -331,7 +338,8 @@
 CFWriteStreamRef _CFWriteStreamCreateFromFileDescriptor(CFAllocatorRef alloc, int fd);
 
 static inline void 
-prepare_io_from_fd(rb_io_t *io_struct, int fd, int mode, bool should_close_streams)
+prepare_io_from_fd(rb_io_t *io_struct, int fd, int mode,
+	bool should_close_streams)
 {
     // TODO we should really get rid of these FMODE_* constants and instead
     // always use the POSIX ones.
@@ -381,6 +389,9 @@
 		CFReadStreamOpen(r);
 		GC_WB(&io_struct->readStream, r);
 		CFMakeCollectable(r);
+		if (!should_close_streams) {
+		    rb_ary_push(kept_streams, (VALUE)r);
+		}
 	    } 
 	    else {
 		io_struct->readStream = NULL;
@@ -398,6 +409,9 @@
 		CFWriteStreamOpen(w);
 		GC_WB(&io_struct->writeStream, w);
 		CFMakeCollectable(w);
+		if (!should_close_streams) {
+		    rb_ary_push(kept_streams, (VALUE)w);
+		}
 	    } 
 	    else {
 		io_struct->writeStream = NULL;
@@ -2456,14 +2470,6 @@
 	if (io_s->should_close_streams) {
 	    io_struct_close(io_s, true, true);
 	}
-	else {
-	    if (io_s->readStream != NULL) {
-		CFRetain(io_s->readStream);
-	    }
-	    if (io_s->writeStream != NULL) {
-		CFRetain(io_s->writeStream);
-	    }
-	}
 	fd = dup2(other->fd, fd);
 	if (fd < 0) {
 	    rb_sys_fail("dup2() failed");
@@ -3653,7 +3659,7 @@
     rb_scan_args(argc, argv, "02", &ext_enc, &int_enc);
 
     int fd[2] = {-1, -1};
-    if (pipe(fd) == -1) {
+    if (pipe(fd) != 0) {
 	rb_sys_fail("pipe() failed");
     }
 
@@ -4557,6 +4563,9 @@
     rb_objc_define_method(rb_cIO, "internal_encoding", rb_io_internal_encoding, 0);
     rb_objc_define_method(rb_cIO, "set_encoding", rb_io_set_encoding, -1);
 
+    kept_streams = rb_ary_new();
+    rb_objc_retain((void *)kept_streams);
+
     rb_stdin = prep_io(fileno(stdin), FMODE_READABLE, rb_cIO, false);
     GC_WB(&(ExtractIOStruct(rb_stdin)->path), CFSTR("<STDIN>"));
     rb_define_variable("$stdin", &rb_stdin);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090731/3e8bca1d/attachment-0001.html>


More information about the macruby-changes mailing list