我遇到了内存泄漏,但我不知道为什么

时间:2018-12-19 07:27:53

标签: c++ memory-management memory-leaks dr-memory

我无法确定是否确实缺少某些东西,但是我正在写一个小游戏,使用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>)

我该如何解决?有人可以解释这个错误试图超越存在内存泄漏的事实吗?谢谢!

1 个答案:

答案 0 :(得分:2)

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

"SELECT * FROM users WHERE last_login NOT BETWEEN '" . $fromdate . "' AND '" . $todate . "'"

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

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