[Rd] methods package PROTECT "bug"

Seth Falcon sfalcon at fhcrc.org
Thu Mar 15 06:15:57 CET 2007


Hi,

src/library/methods/src/methods_list_dispatch.c has the following code
which is part of the definition of R_dispatchGeneric:

          /*  get its class */
            SEXP arg; int check_err;
            PROTECT(arg = R_tryEval(arg_sym, ev, &check_err));
            if(check_err)
                error(_("error in evaluating the argument '%s' in selecting a me thod for function '%s'"),
                      CHAR(PRINTNAME(arg_sym)),CHAR(asChar(fname)));
            PROTECT(thisClass = R_data_class(arg, TRUE)); nprotect++;
            UNPROTECT(1); /* for arg */
        }
    SET_VECTOR_ELT(classes, i, thisClass);
    lwidth += strlen(STRING_VALUE(thisClass)) + 1;

Based on the comment and the fact that nprotect gets incremented, I
think the wrong object is being unprotected.  It doesn't really matter
since thisClass will get protection by being placed in the classes
vector.  But I think the code would be easier to read with a patch
like the following:

--- a/src/library/methods/src/methods_list_dispatch.c
+++ b/src/library/methods/src/methods_list_dispatch.c
@@ -837,8 +837,9 @@ SEXP R_dispatchGeneric(SEXP fname, SEXP ev, SEXP fdef)
            if(check_err)
                error(_("error in evaluating the argument '%s' in selecting a method for function '%s'"),
                      CHAR(PRINTNAME(arg_sym)),CHAR(asChar(fname)));
-           PROTECT(thisClass = R_data_class(arg, TRUE)); nprotect++;
+           thisClass = R_data_class(arg, TRUE);
            UNPROTECT(1); /* for arg */
+           PROTECT(thisClass); nprotect++;
        }
     SET_VECTOR_ELT(classes, i, thisClass);
     lwidth += strlen(STRING_VALUE(thisClass)) + 1;


-- 
Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center
http://bioconductor.org



More information about the R-devel mailing list