From radford at cs.toronto.edu  Thu Jun 26 22:36:24 2014
From: radford at cs.toronto.edu (Radford Neal)
Date: Thu, 26 Jun 2014 16:36:24 -0400
Subject: [Rd]  Need help on calling Head from C
Message-ID: <20140626203624.GA17415@cs.toronto.edu>

>> PROTECT(dfm=lang3(install("data.frame"),df,ScalarLogical(FALSE)));
>> SET_TAG(CDDR(dfm), install("stringsAsFactors")) ;
>> SEXP res = PROTECT(eval(dfm,R_GlobalEnv));
>> PROTECT(head=lang3(install("head"),res,ScalarInteger(1)));
>> head  = PROTECT(eval(head,R_GlobalEnv));
>> 
>> 
>> I tried the above following , it seemed to be not working . Can you please
>> help.
>> 
>
>Can you elaborate? The above code works AFAICT ...

The code is actually not safe.  Both "install" and "SalarLogical/Integer"
potentially allocate memory, so at least one needs to be protected before
callling lang3.  (Passing one such argument would be OK, since lang3 
protects its arguments, but it doesn't get a chance to do that while they
are still being evaluated.)

Now, I'm not sure this is the source of the actual problem, since both
"data.frame" and "head" presumably already exist, so the install won't
actually allocate memory.  But this is not a safe coding method in 
general.

   Radford Neal



[Rd] Need help on calling Head from C
Radford Neal