Access violation using wxDynamicLibrary
Posted: Fri Jan 08, 2010 11:02 pm
I have finally been able to successfully create and load a DLL using wxWidgets. I created a simple app using a lot of global variables to load and unload the DLL like this
Now I am tring to do away with the globals and set it up so that I can load multiple DLLs using wxDynamicLibrary::Detach()... So I changed a few lines
Now I am getting an access violation as the app is shutting down.
I can only guess that it has something to do with how I am using the Detach member function, but I am not quite getting it.
Can anyone tell me what I am doing wrong?
Code: Select all
const wxString g_FunctionRegister = wxT("RegisterPlugin");
const wxString g_FunctionUnregister = wxT("UnregisterPlugin");
IPlugin *g_pPlugin = NULL;
wxDynamicLibrary LibLoader;
DLLFunctionPtr pFuncRegisterPlugin = NULL;
DLLFunctionPtr pFuncUnregisterPlugin = NULL;
void CMainFrame::LoadPlugin(const wxString &Filename)
{
if (!LibLoader.Load(Filename, wxDL_NOW) )
{
wxMessageBox(wxT("Could not load Plugin"), wxT("Error"), wxOK, this);
return;
}
if(LibLoader.IsLoaded() )
{
if(LibLoader.HasSymbol(g_FunctionRegister) && LibLoader.HasSymbol(g_FunctionUnregister) )
{
pFuncRegisterPlugin = (DLLFunctionPtr)(LibLoader.GetSymbol(g_FunctionRegister) );
pFuncUnregisterPlugin = (DLLFunctionPtr)(LibLoader.GetSymbol(g_FunctionUnregister) );
if(pFuncRegisterPlugin)
{
if (pFuncRegisterPlugin(&g_pPlugin) )
{
g_pPlugin->InitPlugin(this);
}
}
else
{
wxMessageBox(wxT("GetSymbol returned NULL"), wxT("Error"), wxOK, this);
}
}
else
{
wxMessageBox(wxT("HasSymbol returned NULL"), wxT("Error"), wxOK, this);
}
}
}
void CMainFrame::UnloadAllPlugins()
{
if (g_pPlugin)
{
if(pFuncUnregisterPlugin)
{
pFuncUnregisterPlugin(&(g_pPlugin) );
}
}
}
Code: Select all
wxDllType g_LibHandle = 0;
void CMainFrame::LoadPlugin(const wxString &Filename)
{
...
if (pFuncRegisterPlugin(&g_pPlugin) )
{
g_LibHandle = LibLoader.Detach();
g_pPlugin->InitPlugin(this);
}
...
}
void CMainFrame::UnloadAllPlugins()
{
if (g_pPlugin)
{
if(pFuncUnregisterPlugin)
{
pFuncUnregisterPlugin(&(g_pPlugin) );
}
wxDynamicLibrary::Unload(g_LibHandle);
}
}
I can only guess that it has something to do with how I am using the Detach member function, but I am not quite getting it.
Can anyone tell me what I am doing wrong?