VS2010 reports false memory leaks for static classes in a DLL

前端 未结 5 958
清歌不尽
清歌不尽 2020-12-19 10:14

A follow-up question to Memory leaks when calling ITK from Visual Studio DLL

I refined the problem to the simplest example.

struct A
  {
  public:
           


        
5条回答
  •  余生分开走
    2020-12-19 10:46

    I hit the same symptom in the course of migrating an internal library from static linking to load-time dynamic linking, and it turned out that the problem in my case was that the DLL project and the EXE project were linked to different versions of VC++'s runtime/MFC libraries (one was MBCS and one was Unicode).

    In my case, the application and library were both using MFC, and the _AFX_DEBUG_STATE destructor which activates the CRT memory leak dump was being called twice, for two separate objects -- since the DLL and EXE linked to different runtime DLLs, static state in the runtime was effectively duplicated. One of the DLLs would unload and dump leaks too early and show a bunch of false leaks. Switching both projects to use the same character set resolved the separate runtime linkage and also resolved the false leak reports.

    In my case, linkage to the two separate runtimes was unintentional and may have caused other problems anyway. This obviously wouldn't be the case when consuming third party libraries with a well-defined ABI where you have no control over what CRT the library is linked to.

    Not sure if this would've be applicable in your case but I wanted to post in case it's helpful to others.

提交回复
热议问题