1

即使是最简单的 std::mutex 示例,英特尔 Inspector 也会报告内部内存泄漏:

// std_mutex_test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "TestMutex.h"

int _tmain(int argc, _TCHAR* argv[])
{
    std::cout << "Starting\n";
    CTestMutex testMutex;

    for (int i = 0; i < 10; i++)
        testMutex.DoStuff();

    std::cout << "Done\nPress a key...\n";
    getchar();
    return 0;
}

// TestMutex.h : Class for testing mutex
//
#include "stdafx.h"
#include "TestMutex.h"

class CTestMutex
{
private:
    std::mutex mtx1;
    long sharedStuff;  // Example is just for testing the mutex, otherwise I would use atomic here
public:
    CTestMutex() { sharedStuff = 0; }
    ~CTestMutex();

    void CTestMutex::DoStuff()
    {
        mtx1.lock();
        sharedStuff++;
        mtx1.unlock();
    }

};

Intel Inspector XE 2013 报告如下:

ID      Type    Sources Modules Object Size State
P1      Kernel resource leak    schedulerbase.cpp   std_mutex_test.exe      New
P2      Memory leak cachelocalschedulegroup.h   std_mutex_test.exe  304 New
P3      Memory leak collections.h   std_mutex_test.exe  7168    New
P4      Memory leak collections.h   std_mutex_test.exe  84  New
P5      Memory leak collections.h   std_mutex_test.exe  14336   New
P6      Memory leak collections.h   std_mutex_test.exe  4   New
P7      Memory leak collections.h   std_mutex_test.exe  56  New
P8      Memory leak collections.h   std_mutex_test.exe  12  New
P9      Memory leak collections.h   std_mutex_test.exe  96  New
P10     Memory leak collections.h   std_mutex_test.exe  49192   New
P11     Memory leak collections.h   std_mutex_test.exe  288 New
P12     Memory leak contextbase.h   std_mutex_test.exe  16  New
P13     Memory leak externalcontextbase.cpp std_mutex_test.exe  28  New
P14     Memory leak mailbox.h   std_mutex_test.exe  44  New
P15     Memory leak mailbox.h   std_mutex_test.exe  256 New
P16     Memory leak resourcemanager.cpp std_mutex_test.exe  12  New
P17     Memory leak resourcemanager.cpp std_mutex_test.exe  44  New
P18     Memory leak resourcemanager.cpp std_mutex_test.exe  52  New
P19     Memory leak resourcemanager.cpp std_mutex_test.exe  416 New
P20     Memory leak resourcemanager.cpp std_mutex_test.exe  208 New
P21     Memory leak resourcemanager.cpp std_mutex_test.exe  144 New
P22     Memory leak resourcemanager.cpp std_mutex_test.exe  64  New
P23     Memory leak rminternal.h    std_mutex_test.exe  64  New
P24     Memory leak rminternal.h    std_mutex_test.exe  8   New
P25     Memory leak rminternal.h    std_mutex_test.exe  292 New
P26     Memory leak schedulerbase.cpp   std_mutex_test.exe  176 New
P27     Memory leak schedulerbase.cpp   std_mutex_test.exe  56  New
P28     Memory leak schedulerbase.cpp   std_mutex_test.exe  20  New
P29     Memory leak schedulerbase.cpp   std_mutex_test.exe  4   New
P30     Memory leak schedulerbase.cpp   std_mutex_test.exe  4   New
P31     Memory leak schedulerbase.cpp   std_mutex_test.exe  192 New
P32     Memory leak schedulerbase.cpp   std_mutex_test.exe  136 New
P33     Memory leak schedulerbase.cpp   std_mutex_test.exe  1024    New
P34     Memory leak schedulerpolicybase.cpp std_mutex_test.exe  40  New
P35     Memory leak schedulerproxy.cpp  std_mutex_test.exe  2104    New
P36     Memory leak schedulerproxy.cpp  std_mutex_test.exe  4   New
P37     Memory leak schedulerproxy.cpp  std_mutex_test.exe  704 New
P38     Memory leak structuredworkstealingqueue.h   std_mutex_test.exe  2048    New
P39     Memory leak structuredworkstealingqueue.h   std_mutex_test.exe  4096    New
P40     Memory leak threadscheduler.cpp std_mutex_test.exe  520 New
P41     Memory leak threadscheduler.cpp std_mutex_test.exe  1664    New
P42     Memory not deallocated  trace.cpp   std_mutex_test.exe  24  New

我在这里做错了什么,或者这些已知的内核内部内存泄漏是我无法控制的?

使用英特尔 C++ 编译器使用多线程调试 /MTd 标志编译示例,在 Windows 7 上运行 Visual Studio 2012 Update 3,并使用所有最新更新。

std::lock_guard还泄漏内存:

void CTestMutex::DoStuff()
{
    std::lock_guard<std::mutex> lock(mtx1);
    sharedStuff++;
}

英特尔 Inspector XE 2013std::lock_guard报告:

ID      Type    Sources Modules Object Size State
P1      Kernel resource leak    schedulerbase.cpp   std_mutex_test.exe      Not fixed
P2      Memory leak cachelocalschedulegroup.h   std_mutex_test.exe  304 Not fixed
P3      Memory leak collections.h   std_mutex_test.exe  7168    Not fixed
P4      Memory leak collections.h   std_mutex_test.exe  84  Not fixed
P5      Memory leak collections.h   std_mutex_test.exe  14336   Not fixed
P6      Memory leak collections.h   std_mutex_test.exe  4   Not fixed
P7      Memory leak collections.h   std_mutex_test.exe  56  Not fixed
P8      Memory leak collections.h   std_mutex_test.exe  12  Not fixed
P9      Memory leak collections.h   std_mutex_test.exe  96  Not fixed
P10     Memory leak collections.h   std_mutex_test.exe  49192   Not fixed
P11     Memory leak collections.h   std_mutex_test.exe  288 Not fixed
P12     Memory leak contextbase.h   std_mutex_test.exe  16  Not fixed
P13     Memory leak externalcontextbase.cpp std_mutex_test.exe  28  Not fixed
P14     Memory leak mailbox.h   std_mutex_test.exe  44  Not fixed
P15     Memory leak mailbox.h   std_mutex_test.exe  256 Not fixed
P16     Memory leak resourcemanager.cpp std_mutex_test.exe  12  Not fixed
P17     Memory leak resourcemanager.cpp std_mutex_test.exe  44  Not fixed
P18     Memory leak resourcemanager.cpp std_mutex_test.exe  52  Not fixed
P19     Memory leak resourcemanager.cpp std_mutex_test.exe  416 Not fixed
P20     Memory leak resourcemanager.cpp std_mutex_test.exe  208 Not fixed
P21     Memory leak resourcemanager.cpp std_mutex_test.exe  144 Not fixed
P22     Memory leak resourcemanager.cpp std_mutex_test.exe  64  Not fixed
P23     Memory leak rminternal.h    std_mutex_test.exe  64  Not fixed
P24     Memory leak rminternal.h    std_mutex_test.exe  8   Not fixed
P25     Memory leak rminternal.h    std_mutex_test.exe  292 Not fixed
P26     Memory leak schedulerbase.cpp   std_mutex_test.exe  176 Not fixed
P27     Memory leak schedulerbase.cpp   std_mutex_test.exe  56  Not fixed
P28     Memory leak schedulerbase.cpp   std_mutex_test.exe  20  Not fixed
P29     Memory leak schedulerbase.cpp   std_mutex_test.exe  4   Not fixed
P30     Memory leak schedulerbase.cpp   std_mutex_test.exe  4   Not fixed
P31     Memory leak schedulerbase.cpp   std_mutex_test.exe  192 Not fixed
P32     Memory leak schedulerbase.cpp   std_mutex_test.exe  136 Not fixed
P33     Memory leak schedulerbase.cpp   std_mutex_test.exe  1024    Not fixed
P34     Memory leak schedulerpolicybase.cpp std_mutex_test.exe  40  Not fixed
P35     Memory leak schedulerproxy.cpp  std_mutex_test.exe  2104    Not fixed
P36     Memory leak schedulerproxy.cpp  std_mutex_test.exe  4   Not fixed
P37     Memory leak schedulerproxy.cpp  std_mutex_test.exe  704 Not fixed
P38     Memory leak structuredworkstealingqueue.h   std_mutex_test.exe  2048    Not fixed
P39     Memory leak structuredworkstealingqueue.h   std_mutex_test.exe  4096    Not fixed
P40     Memory leak threadscheduler.cpp std_mutex_test.exe  520 Not fixed
P41     Memory leak threadscheduler.cpp std_mutex_test.exe  1664    Not fixed
P42     Memory not deallocated  trace.cpp   std_mutex_test.exe  24  Not fixed
4

0 回答 0