I'm working on a plugin system for my application. The plugins are in the form of a dll loaded by my application using LoadLibraryEx
. I store information about each plugin in an arrray of structs:
typedef struct _InternalPluginInfo
{
UINT ID;
HMODULE Module;
int Flags;
PluginInfo Info;
} InternalPluginInfo;
But when I try to remove a plugin from the array with the following function I get a "memory could not be read" error from Windows. When there is only 1 plugin in the array, it works. When there are 2, it fails with that error. When there are 3 or more it seems to work also (altought if there are a lot of plugins it fails again).
int PluginManager::RemovePlugin(UINT ID)
{
err = 0;
UINT index = FindPlugin(ID);
if(err == 0)
{
UINT k = 0;
InternalPluginInfo* temp = new InternalPluginInfo[Count - 1];
if(!temp)
{
err = 2;
return FAILURE;
}
for(UINT i = 0; i < Count; i++)
{
if(Plugin[i].ID != ID)
{
temp[k].ID = Plugin[i].ID;
temp[k].Flags = Plugin[i].Flags;
temp[k].Module = Plugin[i].Module;
temp[k].Info = Plugin[i].Info;
k++;
}
}
if(Plugin[index].Info.PluginName)
delete [] Plugin[index].Info.PluginName;
if(Plugin[index].Info.PluginAuthor)
delete [] Plugin[index].Info.PluginAuthor;
if(Plugin[index].Info.Callback)
(Plugin[index].Info.Callback)(cPluginQuit, 0, 0);
FreeLibrary(Plugin[index].Module);
delete [] Plugin;
Plugin = temp;
Count = k;
return SUCCESS;
}
err = 1;
return FAILURE;
}
I think the error occurs beacuse of this line temp[k].Module = Plugin[i].Module;
, but I'm not sure. When I remove it from the code, everything seems to work fine.
Thanks in advance.