Re: [Rd] protection needed?

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Tue 25 Apr 2006 - 16:49:59 GMT

On Tue, 25 Apr 2006, Hans-Peter wrote:

> Dear list,
>
> I am unsure if I really need to protect a coerced object if I
> immediately access its value and don't access the object anymore. I
> tried to find out by looking into the R sources but didn't find
> something similar.
>
> It's about the variable _skipLines in the call "function ReadXls(
> _file, _sheet, _type, _header, _colHeader, _skipLines: pSExp ): pSExp;
> cdecl;".
>
> I did:
> skipLines:= riInteger( riCoerceVector( _skipLines, setIntSxp ) )[0];
>
> but am unsure if I would have to something along:
> skipInt:= riProtect( riCoerceVector( _skipLines, setIntSxp ) );
> skipLines:= riInteger( skipInt )[0];
> riUnprotect( 1 );
>

Well, assuming that I am guessing correctly about the code (?Pascal), either is ok.

You don't do anything to the output of riCoerceVector() that could cause garbage collection before you stop using it, so it doesn't need protection.

Now, this assumes that you know riInteger() doesn't garbage collect (and won't in the future). The former is definitely true and the latter is a reasonably safe bet.

There is a paragraph in Writing R Extensions section 5.7.1 that advises extreme caution when making this kind of assumption. That's because it's easy to overlook problems, things might change, and it can be really painful to track down pointer protection bugs. It is usually prudent to code as if the garbage collector is out to get you, unless you are doing deep recursion or other types of coding where running out of pointer protection stack is a real concern.

         -thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley@u.washington.edu	University of Washington, Seattle

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed Apr 26 02:52:05 2006

This archive was generated by hypermail 2.1.8 : Tue 25 Apr 2006 - 18:17:44 GMT