[Rd] reg.finalizer and connection gc -- which runs when (and why)?

From: Martin Morgan <mtmorgan_at_fhcrc.org>
Date: Thu, 21 Jul 2011 21:29:51 -0700


With this set-up

options(warn = 1)
tf <- tempfile()
finalizer <- function(obj) {

     message("finalizer")
     close(obj$f)

}

this code works

ev <- new.env()
ev$f <- file(tf, "w")
reg.finalizer(ev, finalizer)
rm(ev)
gc()

whereas this (reversing the order of file() and reg.finalizer())

ev <- new.env()
reg.finalizer(ev, finalizer)
ev$f <- file(tf, "w")
rm(ev)

produces

> gc()

Warning: closing unused connection 3 (/tmp/Rtmp9CWLtN/file6998ee7b) finalizer
Error in close.connection(obj$f) : invalid connection

In some respects, it seems like the user should get a chance to clean up their mess before the system does it for them, so the above seems like a bug. But maybe there is another way to understand this?

Martin

-- 
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Fri 22 Jul 2011 - 04:31:38 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 22 Jul 2011 - 13:30:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive