4

我不知道我是否遗漏了一些非常明显的东西,但是我正在编写一个小游戏,并且使用 DrMemory 时出现了一些内存泄漏。我无法弄清楚到底出了什么问题,所以我编写了一个更简单的文件,该文件有点模拟我的游戏对内存所做的事情。

class A{
public:
    A() { a = new int[10]; };
    ~A() { delete[] a; };
private:
    int* a;
};

int main()
{
    A a;

    return 0;
}

DrMemory 仍然告诉我我有两个字节的内存泄漏,我真的不知道是什么原因造成的。这是生成的错误消息

Error #1: LEAK 2 direct bytes 0x00da0c98-0x00da0c9a + 0 indirect bytes
# 0 replace_malloc                            [d:\drmemory_package\common\alloc_replace.c:2576]
# 1 msvcrt.dll!_strdup   
# 2 .text                                     [../../../src/gcc-6.3.0/libgcc/config/i386/cygwin.S:184]
# 3 __mingw_glob                              [../../../src/gcc-6.3.0/libgcc/config/i386/cygwin.S:184]
# 4 _setargv     
# 5 .text        
# 6 mainCRTStartup
# 7 ntdll.dll!RtlInitializeExceptionChain    +0x62     (0x77849802 <ntdll.dll+0x39802>)
# 8 ntdll.dll!RtlInitializeExceptionChain    +0x35     (0x778497d5 <ntdll.dll+0x397d5>)

我该如何解决?除了存在内存泄漏的事实之外,有人可以解释错误试图说什么吗?谢谢!

4

1 回答 1

2

没有内存泄漏,但可能是误报。
这是我运行 Dr. Memory 时得到的结果:

Dr. Memory version 1.11.0 build 2 built on Aug 29 2016 02:42:07
Dr. Memory results for pid 10412: "memoryleakcheck.exe"
Application cmdline: "C:\Users\user\dev\cpptests\memoryleakcheck\Debug\memoryleakcheck.exe"
Recorded 115 suppression(s) from default C:\Temp\DrMemory-Windows-1.11.0-2\bin\suppress-default.txt

Error #1: INVALID HEAP ARGUMENT to free 0x01d000f8
# 0 replace_free                      [d:\drmemory_package\common\alloc_replace.c:2706]
# 1 memoryleakcheck.exe!?            +0x0      (0x0028231e <memoryleakcheck.exe+0x1231e>)
# 2 memoryleakcheck.exe!?            +0x0      (0x00281aec <memoryleakcheck.exe+0x11aec>)
# 3 memoryleakcheck.exe!?            +0x0      (0x00281933 <memoryleakcheck.exe+0x11933>)
# 4 memoryleakcheck.exe!?            +0x0      (0x00281a46 <memoryleakcheck.exe+0x11a46>)
# 5 memoryleakcheck.exe!?            +0x0      (0x0028222e <memoryleakcheck.exe+0x1222e>)
# 6 memoryleakcheck.exe!?            +0x0      (0x00282097 <memoryleakcheck.exe+0x12097>)
# 7 memoryleakcheck.exe!?            +0x0      (0x00281f2d <memoryleakcheck.exe+0x11f2d>)
# 8 memoryleakcheck.exe!?            +0x0      (0x002822a8 <memoryleakcheck.exe+0x122a8>)
# 9 KERNEL32.dll!BaseThreadInitThunk +0x23     (0x76c762c4 <KERNEL32.dll+0x162c4>)
Note: @0:00:00.193 in thread 13020
Note: refers to -1 byte(s) before next malloc
Note: next higher malloc: 0x01d000f8-0x01d00120
Note: refers to -40 byte(s) beyond last valid byte in prior malloc
Note: prev lower malloc:  0x01d000f8-0x01d00120

Error #2: LEAK 40 direct bytes 0x01d000f8-0x01d00120 + 0 indirect bytes
# 0 replace_malloc                    [d:\drmemory_package\common\alloc_replace.c:2576]
# 1 memoryleakcheck.exe!?            +0x0      (0x002822bd <memoryleakcheck.exe+0x122bd>)
# 2 memoryleakcheck.exe!?            +0x0      (0x00281acc <memoryleakcheck.exe+0x11acc>)
# 3 memoryleakcheck.exe!?            +0x0      (0x00281884 <memoryleakcheck.exe+0x11884>)
# 4 memoryleakcheck.exe!?            +0x0      (0x00281a34 <memoryleakcheck.exe+0x11a34>)
# 5 memoryleakcheck.exe!?            +0x0      (0x0028222e <memoryleakcheck.exe+0x1222e>)
# 6 memoryleakcheck.exe!?            +0x0      (0x00282097 <memoryleakcheck.exe+0x12097>)
# 7 memoryleakcheck.exe!?            +0x0      (0x00281f2d <memoryleakcheck.exe+0x11f2d>)
# 8 memoryleakcheck.exe!?            +0x0      (0x002822a8 <memoryleakcheck.exe+0x122a8>)
# 9 KERNEL32.dll!BaseThreadInitThunk +0x23     (0x76c762c4 <KERNEL32.dll+0x162c4>)

===========================================================================
FINAL SUMMARY:

DUPLICATE ERROR COUNTS:

SUPPRESSIONS USED:

ERRORS FOUND:
      0 unique,     0 total unaddressable access(es)
      0 unique,     0 total uninitialized access(es)
      1 unique,     1 total invalid heap argument(s)
      0 unique,     0 total GDI usage error(s)
      0 unique,     0 total handle leak(s)
      0 unique,     0 total warning(s)
      1 unique,     1 total,     40 byte(s) of leak(s)
      0 unique,     0 total,      0 byte(s) of possible leak(s)
ERRORS IGNORED:
      6 potential error(s) (suspected false positives)
         (details: C:\Temp\DrMemory-Windows-1.11.0-2\drmemory\logs\DrMemory-memoryleakcheck.exe.10412.000\potential_errors.txt)
     17 unique,    29 total,   6711 byte(s) of still-reachable allocation(s)
         (re-run with "-show_reachable" for details)
Details: C:\Temp\DrMemory-Windows-1.11.0-2\drmemory\logs\DrMemory-memoryleakcheck.exe.10412.000\results.txt

我创建新项目时使用默认设置编译

/JMC /permissive- /GS /analyze- /W3 /Zc:wchar_t /ZI /Gm- /Od /sdl /Fd"Debug\vc141.pdb" /Zc:inline /fp:precise /D "_MBCS" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /Oy- /MDd /FC /Fa"Debug\" /EHsc /nologo /Fo"Debug\" /Fp"Debug\memoryleakcheck.pch" /diagnostics:classic 

和链接器:

/OUT:"C:\Users\user\dev\cpptests\memoryleakcheck\Debug\memoryleakcheck.exe" /MANIFEST /NXCOMPAT /PDB:"C:\Users\user\dev\cpptests\memoryleakcheck\Debug\memoryleakcheck.pdb" /DYNAMICBASE "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG:FASTLINK /MACHINE:X86 /INCREMENTAL /PGD:"C:\Users\user\dev\cpptests\memoryleakcheck\Debug\memoryleakcheck.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"Debug\memoryleakcheck.exe.intermediate.manifest" /ERRORREPORT:PROMPT /NOLOGO /TLBID:1 
于 2018-12-19T08:23:40.393 回答