[Rd] PROTECT help
Duncan Murdoch
murdoch.duncan at gmail.com
Tue Mar 27 15:49:15 CEST 2012
On 12-03-27 9:22 AM, Terry Therneau wrote:
> I received the following note this AM. The problem is, I'm not quite
> sure how to fix it.
> Can one use PROTECT(coxlist(eval(PROTECT.... , do I create an
> intermediate variable, or otherwise?
I think both would work. The usual style in R sources is to use an
intermediate variable, assigned within the PROTECT call, e.g.
PROTECT(var = f());
but this would act the same as
var = PROTECT(f());
I don't know where the best docs are, but here is my understanding of
PROTECT:
What PROTECT(x) does is to make a copy of the pointer x in a stack of
protected pointers. When garbage collection happens, nothing in that
stack will be released. It is safe to protect things that don't need
protection, but it is a little inefficient. (You shouldn't call
PROTECT() on a pointer that isn't an R object declared as a SEXP, but it
will only cause trouble in certain debugging modes.)
PROTECT(x) does return the value of x, so f(PROTECT(x)) should evaluate
the same as f(x) (but x will be protected from collection).
The main thing to watch when you use PROTECT is that you keep track of
how many times it is called, because UNPROTECT just pops a number of
pointers off the protection stack.
>
> I'm willing to update the code if someone will give me a pointer to the
> right documentation. This particular chunk was written when there was a
> lot of change going on in the callback mechanism and so there might be a
> safer and/or simpler and/or more standard aproach by now. The routine in
> question has to do with penalized Cox models, the C code needs to get
> the value of the penalty and the penalty is an arbitrary S expression
> passed down from top level.
>
> Terry T
>
> ----------------------------
>
> In survival_2.36-12 (and earlier), in the function cox_callback() at
> cox_Rcallback.c:40:
>
> PROTECT(coxlist=eval(lang2(fexpr,data),rho));
>
> the return value of the call to lang2() is vulnerable if allocations
> within eval() give rise to garbage collection.
>
> (Discovered during CXXR development.)
>
> Andrew
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list