- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经实现了一个包含 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/
在多个动态链接库的解决方案中,每次基本上只有一个库“MyLibrary.dll”会发生堆损坏,但对于“MyOtherLibrary.dll”则不会。相关代码: HINSTANCE hModule; h
我正在编写一个 DLL,需要多次动态调用一个单独的 DLL。我想保持被调用者加载,然后在我的 DLL 卸载时卸载它。但根据微软的说法,这是一个 bad idea . The entry point f
大家好! 我写了一些我在我的项目中使用的dll。在 I 类的构造函数中加载库 lib = LoadLibrary(L"library.dll");,在析构函数中我使用释放它如果(库) 自由库(lib)
我没有在文档中找到这个:如果我需要一个 DLL 处理程序直到程序结束,我是否仍然应该在我的调用程序结束之前调用 FreeLibrary,还是不需要? 最佳答案 最佳做法是始终为任何相应的LoadL
我已经实现了一个包含 DllMain() 入口函数的 DLL:- BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call
我在现场得到了 drwtsn32 生成的故障转储,显示应用程序在调用系统 API FreeLibrary 时被阻止。这是类堆栈: ChildEBP RetAddr Args to Child 06f
我将在运行时加载共享库的依赖于操作系统的 API 包装在一个可移植的接口(interface)中。 问题是,在记录 close() 函数时,我注意到没有关于 dlclose() 或 FreeLibra
我必须在 Delphi XE7 中编写一个 DLL。我想在 DLL 中使用 TParallel.For。 DLL 被加载到 C++ 应用程序中,一切正常。但是,当应用程序终止或调用 FreeLibra
问题 我有一个第三方 DLL,当它试图从我的 native C 应用程序中卸载它时抛出一个未处理的异常。这导致调用 FreeLibrary失败,并且该模块仍在我的进程中加载。 是否有强制卸载库的选
我正在编写一个代理库(称为库 A),它只是与系统上可能存在或不存在的另一个 DLL(称为库 B)的接口(interface)。这个想法是程序将链接到这个库 A 而不是原始库 B ;如果系统上没有安装库
我很震惊地跟踪这个简单的代码: #include void foo() { for (int i = 0; i >(const std::chrono::duration > & _Rel
我想创建一个 dll,在某些情况下自行卸载,这意味着 BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID
我正在查看某人的代码(也许是你的 :D ),其中有一个名为 FreeLibrary() 的函数。 我想知道这个函数是否也适用于 linux。实际上,我尝试用谷歌搜索它,但只得到一些与 Windows
从MSDN文档中可以看出,在DllMain入口函数中不应该调用LoadLibrary/FreeLibrary。 The entry-point function should perform only
我有一个 dll,它导出一个返回接口(interface)的函数。 我为 LoadLibrary、GetProcAddress 和 FreeLibrary 函数创建了一个包装器,用于调用导出的函数。
我今天遇到了这个错误,原来是因为我在调用 FreeLibrary() 后使用了一个字符串从我的 DLL 中分配。 这是一个重现崩溃的简单示例。这进入 DLL: void dllFunc(char **
我有一个应该转换为库的应用程序。我只复制了项目 dpr 并更改了源文件: library aLibrary; uses FastMM4, Forms, SysUtils, Wind
我需要使用 LoadLibrary 和 GetProcAddress 从某些 dll(插入我的应用程序)中调用函数,但我不希望它们能够通过包含一个无限循环或挂起自己来挂起我的程序,所以我试图使用 bo
我正在开发一个与 native DLL 交互的 Web 服务,我使用 LoadLibrary/GetModuleHandle/FreeLIbrary 和 GetProcAddress 来动态加载/卸载
这是我在 DLL 中的代码: procedure TTaskTest; begin TTask.Run( procedure begin Sleep(300);
我是一名优秀的程序员,十分优秀!