C++11 函数async泄漏Windows上的内存

C++11 函数async泄漏Windows上的内存,c++11,asynchronous,memory-leaks,visual-studio-2013,dr-memory,C++11,Asynchronous,Memory Leaks,Visual Studio 2013,Dr Memory,在我的一个应用程序中,当我开始使用std::async时,引入了内存泄漏。在检查了Dr.memory工具的问题之后,我发现std::async经常泄漏内存 我做了一个快速测试,重现了这个问题: #include <Windows.h> #include <algorithm> #include <future> using namespace std; void testMethod() { for (int i = 0; i < 4; i+

在我的一个应用程序中,当我开始使用std::async时,引入了内存泄漏。在检查了Dr.memory工具的问题之后,我发现std::async经常泄漏内存

我做了一个快速测试,重现了这个问题:

#include <Windows.h>
#include <algorithm>
#include <future>

using namespace std;

void testMethod()
{
    for (int i = 0; i < 4; i++)
        Sleep(10);
}

int main()
{
    for (int i = 0; i < 100; i++)
    {
        auto result = async(launch::async, testMethod);
        result.wait();
    }

    return 1;
}
#包括
#包括
#包括
使用名称空间std;
void testMethod()
{
对于(int i=0;i<4;i++)
睡眠(10);
}
int main()
{
对于(int i=0;i<100;i++)
{
自动结果=异步(启动::异步,testMethod);
result.wait();
}
返回1;
}
上面的代码如何可能泄漏Windows上的内存?这个问题在Linux上不会发生。Dr.memory将生成以下日志:

Error #1: REACHABLE LEAK 40 direct bytes 0x007badb8-0x007bade0 + 0 indirect bytes
# 0 replace_operator_new_nomatch                                   [d:\drmemory_package\common\alloc_replace.c:2732]
# 1 MSVCR120D.dll!Concurrency::details::SubAllocator::Alloc        [f:\dd\vctools\crt\crtw32\concrt\suballocator.cpp:201]
# 2 MSVCR120D.dll!Concurrency::details::ExternalContextBase::Alloc [f:\dd\vctools\crt\crtw32\concrt\externalcontextbase.cpp:238]
# 3 MSVCR120D.dll!Concurrency::Alloc                               [f:\dd\vctools\crt\crtw32\concrt\suballocator.cpp:34]
# 4 Concurrency::details::_AllocBase::operator new                 [c:\program files (x86)\microsoft visual studio 12.0\vc\include\concrt.h:298]
# 5 Concurrency::task<>::_TaskInitWithFunctor<>                    [c:\program files (x86)\microsoft visual studio 12.0\vc\include\ppltasks.h:3982]
# 6 Concurrency::task<>::_TaskInitMaybeFunctor<>                   [c:\program files (x86)\microsoft visual studio 12.0\vc\include\ppltasks.h:4551]
# 7 Concurrency::task<>::task<><>                                  [c:\program files (x86)\microsoft visual studio 12.0\vc\include\ppltasks.h:4198]
# 8 Concurrency::create_task<>                                     [c:\program files (x86)\microsoft visual studio 12.0\vc\include\ppltasks.h:4673]
# 9 std::_Task_async_state<>::_Task_async_state<><>                [c:\program files (x86)\microsoft visual studio 12.0\vc\include\future:906]
#10 std::_Get_associated_state<>                                   [c:\program files (x86)\microsoft visual studio 12.0\vc\include\future:1859]
#11 std::_Async<>      
错误#1:可到达泄漏40个直接字节0x007badb8-0x007bade0+0个间接字节
#0替换\u运算符\u新\u匹配[d:\drmemory\u package\common\alloc\u replace.c:2732]
#1 MSVCR120D.dll!并发::详细信息::子分配程序::Alloc[f:\dd\vctools\crt\crtw32\concrt\SubAllocator.cpp:201]
#2 MSVCR120D.dll!并发::详细信息::ExternalContextBase::Alloc[f:\dd\vctools\crt\crtw32\concrt\ExternalContextBase.cpp:238]
#3 MSVCR120D.dll!并发::Alloc[f:\dd\vctools\crt\crtw32\concrt\suballocator.cpp:34]
#4并发::详细信息::_AllocBase::操作员新建[c:\program files(x86)\microsoft visual studio 12.0\vc\include\concrt.h:298]
#5并发::任务::_TaskInitWithFunctor[c:\program files(x86)\microsoft visual studio 12.0\vc\include\ppltasks.h:3982]
#6并发::任务::_TaskInitMaybeFunctor[c:\program files(x86)\microsoft visual studio 12.0\vc\include\ppltasks.h:4551]
#7并发::任务::任务[c:\program files(x86)\microsoft visual studio 12.0\vc\include\ppltasks.h:4198]
#8并发::创建任务[c:\program files(x86)\microsoft visual studio 12.0\vc\include\ppltasks.h:4673]
#9 std::_任务\异步\状态::_任务\异步\状态[c:\program files(x86)\microsoft visual studio 12.0\vc\include\future:906]
#10 std::_Get_associated_state[c:\program files(x86)\microsoft visual studio 12.0\vc\include\future:1859]
#11标准::_异步

您看到的是一个泄漏还是100个泄漏?我的猜测是,第一个
async
调用初始化一个线程池,该线程池将一直保留到程序终止。所有其他调用都重用池中已经运行的线程。此单例触发您使用的工具中的假阳性。@IgorTandetnik测试代码中存在多个泄漏。上次运行时,每个异步调用都有泄漏。在测试代码中,我在调用每个线程之后加入它。为什么sync会在有其他线程可用的情况下生成这么多线程?@IgorTandetnik我的上一条评论不正确,我没有运行正确的代码。确实只有一个漏洞。谢谢你的帮助!