[Rd] R console freezes after several runs of compiled C code
Simon Urbanek
simon.urbanek at r-project.org
Wed Apr 15 01:55:23 CEST 2009
On Apr 14, 2009, at 4:57 PM, Xiangyang Ye wrote:
> Hi All,
>
> I tried my best to figure out how to deal with the freezing R
> console but
> with no luck. I followed the instructions of calling R_ProcessEvents()
> regularly but still couldn't work it out (the callings were removed
> from the
> below C code).
>
It's not a hang - it's a crash because of a bug in your code. Look at
the last loop - I suspect you meant j<*n and not j<*m otherwise you'll
be corrupting memory.
FWIW if you want to make a GUI responsive, you should be calling
R_CheckUserInterrupt() and not R_ProcessEveents() because the latter
is not available on all platforms.
Cheers,
Simon
> I am using R-2.8.1 on Windows XP service pack 3. The Rtools is
> version 2.9.
> I have the following C code:
>
> void rx(int *n, int *m, int *rxmax,int *rxdate, int *refills,int
> *rxs) {
> int i,j,k, total;
> int cap[*n][*m];
> int rx[*n* *rxmax][*n];
>
> for (i=0;i<*n;i++)
> for (j=0;j<*m;j++)
> cap[i][j]=0;
>
> for (i=0;i<*n* *rxmax;i++) {
> rx[i][0]=*(rxdate+i);
> rx[i][1]=*(refills+i);
> }
>
> for (i=0;i<*n;i=i+1)
> for (j=*rxmax *i;j<(i+1) * *rxmax-1;j++)
> if (rx[j][0] > 0)
> for (k = rx[j][0]; k < rx[j][0] + rx[j][1]; k++){
> if (k<*m)
> cap[i][k]=1; }
>
> for (j=0;j<*m;j++)
> for (i=0,total=0;i<*m;i++) {
> total= total+cap[j][i];
> rxs[j]=total;
> }
> }
>
> And the following R code:
>
> dyn.load("c:/temp/rxc.dll")
> rx=matrix(c(1,5,11,5,21,5,0,0,
> 1,10,21,10,0,0,0,0),nrow=8, byrow=T);
> n=2
> m=31
> x=c(1:n)
> rxmax= dim(rx)[[1]]/n
> xx <- .C("rx",n=as.integer(n),m=as.integer(m),rxmax=as.integer(rxmax),
> rxdate = as.integer(rx[,1]),
> refills = as.integer(rx[,2]),rxs=as.integer(x))
> xx$rxs
>
>
> After I created the .dll file, I was able to load it into R and got
> the
> result that I expected. But if I re-run the code for several times,
> the R
> console will freeze.
>
> Thanks in advance.
>
> Xiangyang
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>
More information about the R-devel
mailing list