[R] Reloading a shared library with dyn.load

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri May 16 08:54:13 CEST 2003


As there is AFAIK no way to unregister entry points, you cannot safely
unload a DLL with registered entry points (which is what your code appears
to do).  So if you want to do this, don't register them,

Package tcltk does unload its DLL (so the Tcl/Tk dlls get released) if 
detached, and so was not modified to register.

Until un-registration is provided, I would not use the registration 
mechanisms during development.

On Fri, 16 May 2003, James Wettenhall wrote:

> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list