[R] How to unload R.dll successfully by FreeLibrary

暮如雪
Thu, 05 Feb 2015 08:01:13 -0000

hi,
    I've some C++ code which try to load R.dll and execute some R command,
then unload R.dll, yet it seems fail to unload it, so second time to call the
function hangs since it is trying to Initialize R again(this is not allowed as
documented). Anyone can tell me why, thanks.
//the following is my c++ code, used in a Visual Studio 2012 Console Application
#define ORAPI__cdecl
typedefBOOL(ORAPI*PFN_Rf_initEmbeddedR)(int argc, char *argv[]);
typedefvoid(ORAPI*PFN_Rf_endEmbeddedR)(int fatal);


staticvoid_simple_test()
{
#define STR_R_DLL_PATH_T("D:\\R\\R-3.1.2\\bin\\i386\\R.dll")
HMODULEm_hRDll = LoadLibrary(STR_R_DLL_PATH);
if ( m_hRDll )
{
PFN_Rf_initEmbeddedR pfnInit = (PFN_Rf_initEmbeddedR)GetProcAddress(m_hRDll,
"Rf_initEmbeddedR");
PFN_Rf_endEmbeddedR pfnEnd = (PFN_Rf_endEmbeddedR)GetProcAddress(m_hRDll,
"Rf_endEmbeddedR");


char*szArgs[] = {"REmbeddedPostgres", "--gui=none",
"--silent"};
BOOL bRet = pfnInit(_countof(szArgs), szArgs); //call second time, this function
will not return
//do something.
pfnEnd(0);
FreeLibrary(m_hRDll); //==> After this, R.dll still in memory, as I can see
in Visual Studio 2012, Module window
}
}
//testing code end
int _tmain(int argc, _TCHAR* argv[])
{
_simple_test(); //first time, seems fine.
_simple_test(); //second time, hangs...
//_start_test_OR_Cache();
//_start_test_OR_Cache();
return 0;
}



????????????????????????????
	[[alternative HTML version deleted]]