[Rd] protect/unprotect howto in C code
Michael Dondrup
michael.dondrup at cebitec.uni-bielefeld.de
Wed May 17 12:09:22 CEST 2006
Hi,
Im currently trying to debug a 'error in unprotect: stack imbalance' problem
and I am curious about two basic questions on the use of PROTECT and
UNPROTECT, which I could not figure out:
- which objects have to be protected, namely, if the code is something like:
SEXP fun, e;
/* get the expression e ... */
fun = eval(e, R_GlobalEnv);
/* or like this?: PROTECT(fun = eval(e, R_GlobalEnv)); */
PROTECT(fun = VECTOR_ELT(fun, 1));
/* do more things with fun ... */
does one need to protect the result of a call to 'eval' immediately? And how
about R_tryEval?
While searching for code examples in the sources, I found both protected evals
and fewer non-protected.
- Can someone give a hint (or some documents) on a way to simplify debugging
such problem in addition to using gdb, please? I thought about temporarily
defining macros such as
#define DEBUG_Protect(x) PROTECT(x); fprintf(stderr, "Protecting in %s, l:
%d\n", __FILE__, __LINE__)
#define UNDEBUG_Protect(x) fprintf(stderr, "Unprotecting %d in %s, l:, %d
\n", x , __FILE__, __LINE__); UNPROTECT(x);
and then replace all calls temporarily in the package source. But there must
be a better way...
Thank you very much
(and my appologies, if this sounds odd to more experineced c programmers ;) )
Michael
More information about the R-devel
mailing list