c++ - 有没有人能够在使用共享 DLL 的 Windows 64 位应用程序上集成 tcmalloc?

标签 c++ memory-leak-detector tcmalloc

我有一个 64 位 Visual Studio 2010(单线程)C++ Windows 应用程序,我试图将它与 tcmalloc 集成,但在使用任何动态链接的 dll 时遇到问题。我将 tcmalloc 链接为静态库。在应用程序开始使用我们共享的 dll 之一之前,tcmalloc 工作得很好。我将解决方案构建为 64 位 DEBUG 应用程序。所有 dll 与 CRT 库的 C/C++ 调试版本(MSVCP100D.dll 和 MVCR100D.dll)的链接。

下面是失败的代码示例。 tcmalloc 被调用用于所有内存分配,但是当调用 delete 时应用程序崩溃。当我在主可执行文件中创建一个函数并将代码复制到那里时,完全相同的代码工作正常,这让我感到非常困惑。

如果有人有在这种情况下使用 tcmalloc 的任何经验,我将不胜感激您的反馈。这对我来说是个谜。它是 dll 的内存模型问题(不同的堆 ??)?我不知道。在我看来,他们正在使用相同的堆。

抱歉,如果这篇文章太长。我试图提供尽可能多的信息。

谢谢。

布鲁斯

更新:作为测试,我将崩溃的共享 dll 更改为静态库,一切正常,直到应用程序使用不同的 dll。因此,无论出于何种原因,tcmalloc 都需要一些额外的步骤来处理共享的 dll。我可以使用 tcmalloc 为内存分析创建我们所有的 dll 静态库,但如果知道还需要做什么才能使用 shared 就更好了 带有 tcmalloc 的 dll。

DLL头文件方法声明: __declspec(dllexport) 静态 std::string GetExecutablePath();

//.cpp实现

string Parameters::GetExecutablePath()

    string  execPathStr;
    char exeFilePath[ MAX_PATH +1];
    if ( GetModuleFileName( NULL, exeFilePath, MAX_PATH ) )
    {
        //The line of code below is where the app crashes.
        //It calls operator new in crt/src/new.cpp. I verified the call to malloc
        //is forwarded to tcmalloc. 
        *execPathStr = string(exeFilePath);* //creates and deletes a temporary and then crashes

        long dir_pos = execPathStr.rfind( FT_DIR_SLASH ) ;
        execPathStr = execPathStr.substr( 0, dir_pos+1 );
    }

    return execPathStr;

}

临时字符串销毁时调用的方法:

~_String_val()
{   
    // destroy the object
    typename _Alloc::template rebind<_Container_proxy>::other _Alproxy(_Alval);
    this->_Orphan_all();
    _Dest_val(_Alproxy, this->_Myproxy);
    **_Alproxy.deallocate(this->_Myproxy, 1);**
    this->_Myproxy = 0;
}


void deallocate(pointer _Ptr, size_type)
{   
    // deallocate object at _Ptr, ignore size
    **::operator delete(_Ptr);**
}

This is where it crashes. the pHead->nBlockUse is 0. 
crt/dbgdel.cpp:

void operator delete(
        void *pUserData
        )
{
    //code omitted for brevity
    /* verify block type */
    **_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));** //crashes here
}

将 tcmalloc 重建为共享 DLL 后,它现在在尝试释放内存时会在不同的地方崩溃。

afxmem.cpp:

void __cdecl operator delete(void* p)
{
#if !defined(_AFX_NO_DEBUG_CRT) && defined(_DEBUG)
        **_free_dbg(p, _NORMAL_BLOCK);** <-------- this function gets called 
#else
        free(p);
#endif
}

dbgheap.c:

extern "C" _CRTIMP void __cdecl _free_dbg(
        void * pUserData,
        int nBlockUse
        )
{
 _mlock(_HEAP_LOCK);

        __try {
            /* allocate the block
             */
            **_free_dbg_nolock(pUserData, nBlockUse);**
        }
   __finally {
            /* unlock the heap
             */
            _munlock(_HEAP_LOCK);
        }
}



extern "C" void __cdecl _free_dbg_nolock(
        void * pUserData,
        int nBlockUse
        )
{
    //code omitted for brevity

    /*
    * If this ASSERT fails, a bad pointer has been passed in. It may be
    * totally bogus, or it may have been allocated from another heap.
    * The pointer MUST come from the 'local' heap.
    */
    **_ASSERTE(_CrtIsValidHeapPointer(pUserData));** <-------- crashes here
}

最佳答案

我们终于让 tcmalloc 在具有共享 dll 的 Windows 64 位平台上工作。感谢 Captain Oblivious 的建议!诀窍是在 Visual Studio 2010 中使用本文所述的调试符号构建发布版本:How to: Debug a Release Build . tcmalloc 与 MFC 中的 CRT 调试堆调用有冲突,例如:_free_dbg。我们从 tcmalloc 分配内存并从 MFC CRT 堆调试调用中释放内存。在发布版本中,该问题消失了。我们做了一些初步测试,tcmalloc 正在生成堆分析调用图。

谢谢。

布鲁斯

关于c++ - 有没有人能够在使用共享 DLL 的 Windows 64 位应用程序上集成 tcmalloc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14243186/

相关文章:

c++ - 线程共享资源C++

c++ - std::map operator[] 如何导致基本数据类型的内存泄漏?

c++ - 将 valgrind 附加到守护进程并为每个守护进程调用收集日志

glibc - tcmalloc 和 glibc 暴露的符号冲突

c++ - 如何追踪 "tcmalloc : large alloc .... "

c++ - 通用功能的最佳方法

c++ - C++ 和 pascal 字符串之间的区别

c++ - 在 uint64_t 位掩码中高效加载/计算/打包 64 个双重比较结果

macos - 如何使用 Homebrew 在 macOS Catalina (10.15) 上安装 Valgrind?

c++ - tcmalloc 的碎片