I have a main program and a DLL library. Both codes can be summarized as below.
// DLL Module (Start)
class DllModuleClass : public IDllModuleInterFace
{
public:
// ...
void Terminate() override
{
// ...
pMainModuleObject->OnDllModuleObjectTerminated(); // Notify the main program that the DLL can be unloaded now.
// Error occurs at this point (as soon as OnDllModuleObjectTerminated() returns), because it frees the DLL module and this region does not exist anymore.
}
// ...
private:
IMainModuleInterFace * pMainModuleObject;
}
IDllModuleInterFace * GetDllModuleClassInstance();
// DLL Module (End)
// Main Module (Start)
class MainModuleClass : public IMainModuleInterFace
{
public:
// ...
void OnDllModuleObjectTerminated() override
{
FreeLibrary(hDllModule); // DLL module is freed from memory here.
} // Tries to go back to `Terminate()` inside the DLL module, but the DLL module is freed now.
// ...
private:
IDllModuleInterFace * pDllModuleObject;
}
// Main Module (End)
In my code, the DLL module calls a function in the main module in order to notify the main module that the DLL can be unloaded. The main module does so, unloads the DLL from the memory. But the caller from the DLL hasn't return yet. So, after unloading the DLL, there is still a function in the DLL which is still running. This causes an obvious and inevitable runtime error.
Can you suggest a proper way of unloading the DLL in this structure?
Aucun commentaire:
Enregistrer un commentaire