gpt4 book ai didi

c++ - FreeLibrary 与隐式卸载 DLL

转载 作者:行者123 更新时间:2023-11-28 03:57:50 28 4
gpt4 key购买 nike

我已经实现了一个包含 DllMain() 入口函数的 DLL:-

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
case DLL_PROCESS_ATTACH:
/* here im doing file memory mapped through CreateFileMapping() API
and using it through MapViewOfFile() API
storing some data structure in the file mapped area */
...
case DLL_THREAD_ATTACH:
...
case DLL_THREAD_DETACH:
...
case DLL_PROCESS_DETACH:
/* Here unmapping first through UnmapViewOfFile() API
then tries to access the stroed data structure which is illegal
as we have already closed the file mapping
This is the buggy code. The order should be first access the mapped memory
region and then unmap it */
cout <<" some message"<<endl;
...

}

不幸的是,我在 DLL_PROCESS_DETACH 案例中犯了一个错误,访问了非法内存(访问冲突)。

我制作了一个示例程序,它使用 LoadLibrary() 函数加载库,使用库函数,最后调用 FreeLibrary() 并返回。

当我执行这个程序时,我没有收到任何错误信息。但是,如果我删除 FreeLibrary(),在这种情况下,DLL_PROCESS_DETACH 情况将被隐式执行,这一次它会给出错误对话框,提示存在访问冲突。

为什么调用 FreeLibrary() 来抑制这个错误?或者它在内部处理这个异常?什么是建议的方式。

更新:我添加了 ATTACH 和 DETACH 的详细信息。这可能会帮助我为什么不清楚观察到的行为。使用 FreeLibrary() 调用,我没有收到任何错误消息,但未显示 cout 消息。它似乎也崩溃了但没有被通知。但是,如果我删除 FreeLibrary(),在这种情况下,DLL_PROCESS_DETACH 案例将被隐式执行并显示访问冲突对话框。理想情况下,在前一种情况下,它也应该显示错误。所以我猜 FreeLibrary() 抑制了这个访问冲突错误。

最佳答案

The MSDN page for DllMain有一句话可能有助于解释发生在你身上的事情。

When handling DLL_PROCESS_DETACH, a DLL should free resources such as heap memory only if the DLL is being unloaded dynamically (the lpReserved parameter is NULL). If the process is terminating (the lpvReserved parameter is non-NULL), all threads in the process except the current thread either have exited already or have been explicitly terminated by a call to the ExitProcess function, which might leave some process resources such as heaps in an inconsistent state. In this case, it is not safe for the DLL to clean up the resources. Instead, the DLL should allow the operating system to reclaim the memory.

因此,如果您正在调用 FreeLibrary,它本质上是一个干净的关闭。一切仍处于有效状态。 Dll 创建的任何线程仍然存在。它的所有内存仍然存在。

但如果您只是关闭程序(并且不调用 FreeLibrary),那么它更像是紧急关闭(想想崩溃)。您的 Dll 会收到操作系统而不是应用程序的通知。如果这是崩溃的结果,那么希望内存仍然有效并不是一个好主意。而且堆栈无论如何都消失了。

所以我的猜测是您遇到了这个问题,因为未释放的库的卸载方式不同。

关于c++ - FreeLibrary 与隐式卸载 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2726857/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com