From i.ucar86 at gmail.com  Mon Mar 26 23:46:03 2018
From: i.ucar86 at gmail.com (=?UTF-8?B?ScOxYWtpIMOaY2Fy?=)
Date: Mon, 26 Mar 2018 23:46:03 +0200
Subject: [Rd] Objects not gc'ed due to caching (?) in R's S3 dispatch
 mechanism
Message-ID: <CALEXWq0gGFFf-EN5tUhgJpqF_Z5=1AAV-rokCLbEUxiWg_MjqA@mail.gmail.com>

Hi,

I initially opened an issue in the R6 repo because my issue was with
an R6 object. But Winston (thanks!) further simplified my example, and
it turns out that the issue (whether a feature or a bug is yet to be
seen) had to do with S3 dispatching.

The following example, by Winston, depicts the issue:

print.foo <- function(x, ...) {
  cat("print.foo called\n")
  invisible(x)
}

new_foo <- function() {
  e <- new.env()
  reg.finalizer(e, function(e) message("Finalizer called"))
  class(e) <- "foo"
  e
}

new_foo()
gc() # still in .Last.value
gc() # nothing

I would expect that the second call to gc() should free 'e', but it's
not. However, if we call now *any* S3 method, then the object can be
finally gc'ed:

print(1)
gc() # Finalizer called

So the hypothesis is that there is some kind of caching (?) mechanism
going on. Intended behaviour or not, this is something that was
introduced between R 3.2.3 and 3.3.2 (the first succeeds; from the
second on, the example fails as described above).

Regards,
I?aki

PS: Further discussion and examples in https://github.com/r-lib/R6/issues/140



[Rd] Objects not gc'ed due to caching (?) in R's S3 dispatch mechanism
=?UTF-8?B?ScOxYWtpIMOaY2Fy?=