[Rd] R CMD check: Error in function (env) : could not find function "finalize"

Henrik Bengtsson hb at stat.berkeley.edu
Wed Aug 29 20:56:27 CEST 2007


Hi, thanks Seth and others (I've got some offline replies); all
feedback has been useful indeed.

The short story is that as the author of R.oo I am actually the bad
guy here (but not for long since I'm soon committing a fix for R.oo).

REPRODUCIBLE EXAMPLE #1:
% R --vanilla
> library(R.oo)
R.oo v1.2.8 (2006-06-09) successfully loaded. See ?R.oo for help.
> detach("package:R.oo")
> gc()
Error in function (env)  : could not find function "finalize"
Error in function (env)  : could not find function "finalize"
Error in function (env)  : could not find function "finalize"
Error in function (env)  : could not find function "finalize"
Error in function (env)  : could not find function "finalize"
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 142543  3.9     350000  9.4   350000  9.4
Vcells  82660  0.7     786432  6.0   478255  3.7

REPRODUCIBLE EXAMPLE #2:
Here is another example without R.oo illustrating what is going on.
% R --vanilla
> e <- new.env()
> e$foo <- "foo"
> e$foo <- 1
> e <- new.env()
> e$foo <- 1
> reg.finalizer(e, function(env) { print(ls.str(envir=env)) })
> detach("package:utils")
> rm(e)
> gc()
Error in print(ls.str(envir = env)) : could not find function "ls.str"
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 158213  4.3     350000  9.4   350000  9.4
Vcells  86800  0.7     786432  6.0   478255  3.7


WHY ONLY WHEN RUNNING R CMD CHECK?
So, the problem I had was with 'affxparser' examples failing in R CMD
check, but not when I tested them manually.  Same thing was happening
with the 'pcaMethods' package.  The common denominator was that both
'affxparser' and 'pcaMethods' had R.oo dependent package in
DESCRIPTION/Suggests; 'affxparser' used Suggests: R.utils (which
depends on R.oo), and 'pcaMethods' used Suggests: aroma.light (which
in turn *suggests* R.utils).  To the best of my understanding, when R
CMD check runs examples, it will load *all* suggested packages, and
when done, detach them.  When the garbage collector later runs and
cleans out objects, the generic function finalize() in R.oo called by
the registered finalize hook is not around anymore.  FYI, if you move
the R.oo-dependent package from Suggests: to Depends:, there is no
longer a problem because then the package is never detached.  It all
makes sense.


CONCLUSION:
When registering finalizers for object using reg.finalizer() there is
always the risk of the finalizer code to be broken because a dependent
package has been detached.


SOLUTION:
At least make the finalizer hook function robust against these things.
 For instance, check if required packages are loaded etc, or just add
a tryCatch() statement.  However, since finalizers are typically used
to deallocate resources, much more effort has to be taken to make sure
that is still work, which is not easy.  For instance, one could make
sure to require() the necessary packages in the finalizer, but that
has side effects and it is not necessarily sufficient, e.g. you might
only load a generic function, but the method for a specific subclass
might be in a package out of your control.  Same problem goes with
explicit namespace calls to generic functions, e.g. R.oo::finalize().
If you have more clever suggestions, please let me know.


SOME MORE DETAILS ON R.OO:
This is what R.oo looks like now:

Object <- function (core = NA) {
  this <- core
  attr(this, ".env") <- new.env()
  class(this) <- "Object"
  attr(this, "...instanciationTime") <- Sys.time()
  reg.finalizer(attr(this, ".env"), function(env) finalize(this))
  this
}

finalize.Object <- function(this, ...) {}

finalize <- function(...) UseMethod("finalize")

As you see, when detaching R.oo, finalize() is no longer around.

Lesson learned!

Cheers

Henrik

On 8/28/07, Seth Falcon <sfalcon at fhcrc.org> wrote:
> Hi Henrik,
>
> "Henrik Bengtsson" <hb at stat.berkeley.edu> writes:
>
> > Hi,
> >
> > does someone else get this error message:
> >
> > Error in function (env)  : could not find function "finalize"?
> >
> > I get an error when running examples in R CMD check (v2.6.0; session
> > info below):
> [snip]
> > The error occurs in R CMD check but also when start a fresh R session
> > and run, in this case, affxparser.Rcheck/affxparser-Ex.R.  It always
> > occur on the same line.
>
> So does options(error=recover) help in determining where the error is
> coming from?
>
> If you can narrow it down, gctorture may help or running the examples
> under valgrind.
>
> + seth
>
> --
> Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center
> BioC: http://bioconductor.org/
> Blog: http://userprimary.net/user/
>



More information about the R-devel mailing list