简体   繁体   English

无法检测到静态附加到C ++程序的DLL中的内存泄漏

[英]Cannot detect memory leaks in DLLs statically attached to C++ program

I have a host program that implicitly (with libs and their DLLs) loads DLLs. 我有一个主机程序,该程序隐式地(带有库及其DLL)加载DLL。 When I make a memory leak on purpose, in my host program, the CrtDbg* functions detect the leak. 当我故意使内存泄漏时,在我的宿主程序中, CrtDbg*函数会检测到泄漏。 When I on purpose make a memory leak in one of the DLLs the leak is not detected. 当我故意使其中一个DLL发生内存泄漏时,未检测到泄漏。

Note: in my host, I activate the _CrtDumpMemoryLeaks function after the main function has terminated. 注意:在主机中,我在主要功能终止后激活_CrtDumpMemoryLeaks功能。

More than likely your DLLs are statically linking to the CRT. 您的DLL更有可能静态链接到CRT。 (or they are a retail build, but your EXE is a debug build). (或者它们是零售版本,但您的EXE是调试版本)。 Either way, you have more than one heap. 无论哪种方式,您都有多个堆。 When you call _CrtDeumpMemoryLeaks, it can only track the unreleased memory allocations for the binary that calls is made from. 调用_CrtDeumpMemoryLeaks时,它只能跟踪进行调用的二进制文件的未释放内存分配。 You have two options. 您有两个选择。

  • Change all your code (DLL and EXE) to link to the same MSVCRT DLL instead of static linking. 更改所有代码(DLL和EXE)以链接到相同的MSVCRT DLL,而不是静态链接。 In the project settings for each binary, choose "Multithreaded DEBUG DLL" or "Multithreaded DLL" as the linkage type to the CRT. 在每个二进制文件的项目设置中,选择“多线程调试DLL”或“多线程DLL”作为CRT的链接类型。 Don not choose static. 不要选择静态。 Then all DLLs and the EXE will share the same heap. 然后,所有DLL和EXE将共享同一堆。

OR 要么

  • Export a function out of each DLL called "DetectMemoryLeaks" (or similarly named function for the other DLLs so there's not a naming conflict). 从称为“ DetectMemoryLeaks”的每个DLL中导出一个函数(或为其他DLL命名类似的函数,因此不存在命名冲突)。 This function just calls _CrtDumpMemoryLeaks. 此函数仅调用_CrtDumpMemoryLeaks。 From your EXE, call this function around the same time you call _CrtDumpMemoryLeaks. 从EXE调用_CrtDumpMemoryLeaks的大约同一时间调用此函数。 Each heap from each DLL will get checked for leaks. 每个DLL中的每个堆都将检查泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM