Skip to content
Prev 52605 / 63424 Next

Memory leak with tons of closed connections

Using dup() before fdopen() (and calling fclose() on the connection
when it is closed) indeed fixes the memory leak.

FYI,
Gabor

Index: src/main/connections.c
===================================================================
--- src/main/connections.c (revision 71653)
+++ src/main/connections.c (working copy)
@@ -576,7 +576,7 @@
     fp = R_fopen(name, con->mode);
     } else {  /* use file("stdin") to refer to the file and not the console */
 #ifdef HAVE_FDOPEN
- fp = fdopen(0, con->mode);
+        fp = fdopen(dup(0), con->mode);
 #else
  warning(_("cannot open file '%s': %s"), name,
  "fdopen is not supported on this platform");
@@ -633,8 +633,7 @@
 static void file_close(Rconnection con)
 {
     Rfileconn this = con->private;
-    if(con->isopen && strcmp(con->description, "stdin"))
- con->status = fclose(this->fp);
+    con->status = fclose(this->fp);
     con->isopen = FALSE;
 #ifdef Win32
     if(this->anon_file) unlink(this->name);
On Fri, Nov 11, 2016 at 1:12 PM, G?bor Cs?rdi <csardi.gabor at gmail.com> wrote: