[Rd] Protection stack overflow when calling setGeneric()/setMethod() from within .onLoad()

Hervé Pagès hpages at fhcrc.org
Tue Jul 7 02:25:04 CEST 2009

Hi list,

Calling setGeneric()/setMethod() from within the .onLoad() hook
of MyPkg package produces the following error:

   > gctorture(TRUE)
   > library(MyPkg)
   .onLoad(): ok1
   .onLoad(): ok2
   Error : protect(): protection stack overflow
   Error : .onLoad failed in 'loadNamespace' for 'MyPkg'
   Error: package/namespace load failed for 'MyPkg'

This is with the following code in the .onLoad() hook:

   .onLoad <- function(libname, pkgname) {
     cat(".onLoad(): ok1\n")
     setGeneric("foo", function(x, y) standardGeneric("foo"))
     cat(".onLoad(): ok2\n")
     setMethod("foo", c("ANY", "ANY"),
         function(x, y) cat("calling foo,ANY,ANY method\n")
     cat(".onLoad(): ok3\n")

and with R-2.9.0/R-2.9.1 or recent R-devel.

Note that without the call to gctorture(TRUE), everything *seems* to work

   > library(MyPkg)
   .onLoad(): ok1
   .onLoad(): ok2
   .onLoad(): ok3

but it could be that my session is now corrupted because some users have
reported the nasty and now famous:

   Error in get(name, envir = table) :
     formal argument "envir" matched by multiple actual arguments
   Calls: assign ... .getGeneric -> .cacheGeneric -> .cacheGenericTable -> get

after they've loaded MyPkg and started to run some other code that is not
necessarily calling foo(). Those users are running R-2.9.1 or a very recent
R-devel (less than 1 week old) so they are not triggering the reg.finalizer()
problem reported here (and fixed) a few weeks ago:


Also note that if I make foo() a function of one argument only, I don't
get the protection stack overflow anymore.


Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M2-B876
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319

More information about the R-devel mailing list