Skip to content

R_NilValue blows up on Windows

2 messages · Vele Samak, Brian Ripley

#
Ok, Here's the c code that's loaded into R as dll on windows:

#include <R.h>
#include <Rdefines.h>
SEXP test1(SEXP col);

SEXP test1(SEXP col) 
{
  SEXP col2;
  Rprintf("no value!\n");
  return col2;
}

Compiles ok with bcc32 into a dll, loads into R as part of a library, I
Run library(rkdb), then
no value!

And puf! Rterm crashes. 

What's the point of this? I developed a library for connecting to a kdb
database from within R. Similar to RODBC, but faster and more
streamlined for access of real-time tick data. Everything works great,
compiles on Win2000, R 1.3.1 and R 1.5.1 with Borland's free C++ 5.5,
and runs into .dll. All functions work great except when there's no data
from the database I do 
  if (nrows <= 0) {
    Rprintf("No rows...");
    return R_NilValue;
  }	
And there lies the blow up of Rterm!!! Apparently R_NilValue can be
assigned to any SEXP object internally, but when it's returned, Rterm
doesn't know what to do with it. I tried another bypass: 
	SEXP NIL;
  if (nrows <= 0) {
    Rprintf("No rows...");
    return NIL;
  }		
This works in most but not all cases. Weird! 

Any suggestions, comments are highly appreciated. Thanks,

--
Vele Samak
http://www.velesamak.com 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
There is no such problem, as R itself uses this construction repeatedly.
You are using a non-standard compiler, and also your example is incorrect,
as the recommended compiler warns.
On Thu, 22 Aug 2002, Vele Samak wrote:

            
As it should.  You have returned an unassigned pointer.  Does your
compiler not warn you about such an error?  The recommended one does:

gcc   -Ic:/R/rw1060/src/include -Wall -O2   -c foo.c -o foo.o
foo.c: In function `test1':
foo.c:6: warning: `col2' might be used uninitialized in this function
Are you sure that R_NilValue is being linked correctly?  It is an
exported variable from R.dll.  There is a caveat in `readme.packages'
about this, and it is the usual cause of such problems.
It does if linked as recommended,
What's NIL?