[R] Reloading a shared library with dyn.load

James Wettenhall wettenhall at wehi.edu.au
Fri May 16 07:27:54 CEST 2003


Hi,

I'm using dyn.load to load a shared library (compiled from C 
code) into R.  If I dyn.unload it and then dyn.load it again, I 
get an hourglass icon in Rgui (R 1.7.0, Win 2000), and it 
just sits there forever.  I can't press Escape to stop the 
current computation, but I can close Rgui without resorting to 
using the Task Manager.

Is it a problem with my use of R_alloc?  Do I need something 
like R_cleanup?  Or is it a problem with dyn.load?

I'll demonstrate how I compile a C file, HelloFromC.C into a 
shared library, HelloFromC.dll and then load it into R.

HelloFromC.c
------------
#include <R.h>
#include <Rinternals.h>
#include <R_ext/Rdynload.h>
#include <R_ext/Memory.h>
#include <R_ext/Applic.h>
#include <stdio.h>

void HelloFromC(char **result)
{
   *result = (char *) R_alloc(20,sizeof(char));
   sprintf(*result,"Hello from C!"); 
}

static const
R_CMethodDef CEntries[] = {
{"HelloFromC",(DL_FUNC) &HelloFromC,1},
{NULL,NULL,0}
};

void R_init_HelloFromC(DllInfo *info)
{
    R_registerRoutines(info,CEntries,NULL,NULL,NULL);
}

----------------------------------------------------------------
c:\james\HelloFromC> Rcmd SHLIB HelloFromC
making HelloFromC.d from HelloFromC.c
gcc   -IC:/JAMES/rw1070/src/include -Wall -O2   -c HelloFromC.c 
-o HelloFromC.o
ar cr HelloFromC.a *.o
ranlib HelloFromC.a
gcc  --shared -s  -o HelloFromC.dll HelloFromC.def HelloFromC.a  
-LC:/JAMES/rw1070/src/gnuwin32  -lg2c -lR
-----------------------------------------------------------------

Now in R 1.7.0...
-----------------

> dyn.load("HelloFromC")
> result <- ""
> .C("HelloFromC",result)[[1]]
[1] "Hello from C!"
> dyn.unload("HelloFromC")
> dyn.load("HelloFromC")

This is where it freezes.

Any suggestions?

Regards,
James




More information about the R-help mailing list