0

我刚刚下载了 DrMemory并想检查我的应用程序中的内存泄漏,我发现 DrMemory 在“空程序”上显示内存泄漏!

  1. 这些内存泄漏从何而来?
  2. 其次,初学者经常做错什么导致 DrMemory 抱怨未初始化访问的例子是什么?

请注意,我对 C 和所有本地内容相对较新,所以我不明白 DrMemory 在这里告诉我的大部分内容。

int main() {
}

gcc main.c -o a.exe
drmemory -show_reachable -- a.exe

Dr. Memory version 1.4.6 build 2 built on Mar  7 2012 10:14:04
Application cmdline: ""C:\Users\niklas\Desktop\a.exe""
Recorded 62 suppression(s) from default C:\DrMemory/bin/suppress-default.txt

REACHABLE LEAK 532 direct bytes 0x003e07d0-0x003e09e4 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 1152 direct bytes 0x003e09f0-0x003e0e70 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 8 direct bytes 0x003e11a8-0x003e11b0 + 0 indirect bytes
# 0 msvcrt.dll!towlower  
# 1 msvcrt.dll!towlower  
# 2 msvcrt.dll!clearerr_s
# 3 msvcrt.dll!clearerr_s
# 4 __mingw_CRTStartup
# 5 mainCRTStartup
# 6 KERNEL32.dll!BaseThreadInitThunk
# 7 ntdll.dll!RtlInitializeExceptionChain
# 8 ntdll.dll!RtlInitializeExceptionChain

REACHABLE LEAK 80 direct bytes 0x003e11d8-0x003e1228 + 0 indirect bytes
# 0 __cmshared_create_or_grab                 [../../../../gcc-4.4.1/libgcc/../gcc/config/i386/cygming-shared-data.c:145]
# 1 __gcc_register_frame
# 2 register_frame_ctor
# 3 __do_global_ctors
# 4 __mingw_CRTStartup
# 5 mainCRTStartup
# 6 KERNEL32.dll!BaseThreadInitThunk
# 7 ntdll.dll!RtlInitializeExceptionChain
# 8 ntdll.dll!RtlInitializeExceptionChain

REACHABLE LEAK 544 direct bytes 0x003e1568-0x003e1788 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 1863 direct bytes 0x003e1790-0x003e1ed7 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 4096 direct bytes 0x003e2780-0x003e3780 + 0 indirect bytes
# 0 msvcrt.dll!strcpy_s   +0x5f     (0x76aff5d3 <msvcrt.dll+0xf5d3>)
# 1 msvcrt.dll!wprintf    +0x142    (0x76b15f26 <msvcrt.dll+0x25f26>)
# 2 msvcrt.dll!printf     +0x35     (0x76b0c5ef <msvcrt.dll+0x1c5ef>)
# 3 main 

REACHABLE LEAK 24 direct bytes 0x005307d0-0x005307e8 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE POSSIBLE LEAK 3526 direct bytes 0x005307f0-0x005315b6 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE POSSIBLE LEAK 2678 direct bytes 0x005315c0-0x00532036 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 60 direct bytes 0x00532040-0x0053207c + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 48 direct bytes 0x00532088-0x005320b8 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 120 direct bytes 0x005320c0-0x00532138 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 120 direct bytes 0x00532140-0x005321b8 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 540 direct bytes 0x005321c0-0x005323dc + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 66 direct bytes 0x005323e8-0x0053242a + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE POSSIBLE LEAK 120 direct bytes 0x00532438-0x005324b0 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 16 direct bytes 0x005324b8-0x005324c8 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 70 direct bytes 0x005324d0-0x00532516 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE POSSIBLE LEAK 120 direct bytes 0x00532520-0x00532598 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 16 direct bytes 0x005325b8-0x005325c8 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 16 direct bytes 0x005325a0-0x005325b0 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 66 direct bytes 0x005325d0-0x00532612 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 32 direct bytes 0x00532620-0x00532640 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 100 direct bytes 0x00532648-0x005326ac + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 32 direct bytes 0x005326b8-0x005326d8 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 512 direct bytes 0x005326e0-0x005328e0 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 512 direct bytes 0x005328e8-0x00532ae8 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 36 direct bytes 0x00532af0-0x00532b14 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 36 direct bytes 0x00532b20-0x00532b44 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 48 direct bytes 0x00532b50-0x00532b80 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 32 direct bytes 0x00532b88-0x00532ba8 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 32 direct bytes 0x00532bb0-0x00532bd0 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 32 direct bytes 0x00532bd8-0x00532bf8 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 32 direct bytes 0x00532c28-0x00532c48 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 32 direct bytes 0x00532c50-0x00532c70 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 16 direct bytes 0x00532c78-0x00532c88 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 62 direct bytes 0x00532c90-0x00532cce + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 120 direct bytes 0x00532cd8-0x00532d50 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 16 direct bytes 0x00532d58-0x00532d68 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 16 direct bytes 0x00532d70-0x00532d80 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 16 direct bytes 0x00532d88-0x00532d98 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 16 direct bytes 0x00532da0-0x00532db0 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 520 direct bytes 0x00532de0-0x00532fe8 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 1024 direct bytes 0x00532ff0-0x005333f0 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 32 direct bytes 0x005333f8-0x00533418 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 32 direct bytes 0x00533420-0x00533440 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 32 direct bytes 0x00533448-0x00533468 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 206 direct bytes 0x005334e0-0x005335ae + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 16 direct bytes 0x005336f0-0x00533700 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 100 direct bytes 0x00533708-0x0053376c + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 120 direct bytes 0x00533778-0x005337f0 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 32 direct bytes 0x005337f8-0x00533818 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 62 direct bytes 0x00533820-0x0053385e + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 62 direct bytes 0x00533868-0x005338a6 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 36 direct bytes 0x005338d8-0x005338fc + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 24 direct bytes 0x00533908-0x00533920 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 8 direct bytes 0x00533928-0x00533930 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 1288 direct bytes 0x00533950-0x00533e58 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 888 direct bytes 0x00533e60-0x005341d8 + 0 indirect bytes
# 0 <not in a module> (0x00000000)

REACHABLE LEAK 520 direct bytes 0x00534888-0x00534a90 + 0 indirect bytes
# 0 ntdll.dll!RtlInitializeExceptionChain    +0x1e8    (0x7712b57a <ntdll.dll+0x5b57a>)
# 1 KERNELBASE.dll!GetEnvironmentVariableA   +0x18b    (0x752e7b22 <KERNELBASE.dll+0x7b22>)
# 2 msvcrt.dll!memicmp                       +0x85     (0x76b0074e <msvcrt.dll+0x1074e>)
# 3 msvcrt.dll!ismbblead                     +0x2f     (0x76aff637 <msvcrt.dll+0xf637>)
# 4 msvcrt.dll!ismbblead                     +0x12     (0x76aff61a <msvcrt.dll+0xf61a>)
# 5 msvcrt.dll!clearerr_s                    +0x3fa    (0x76b09fb0 <msvcrt.dll+0x19fb0>)
# 6 msvcrt.dll!clearerr_s                    +0x2e3    (0x76b09e99 <msvcrt.dll+0x19e99>)
# 7 msvcrt.dll!clearerr_s                    +0x27e    (0x76b09e34 <msvcrt.dll+0x19e34>)
# 8 __mingw_CRTStartup
# 9 mainCRTStartup
#10 KERNEL32.dll!BaseThreadInitThunk
#11 ntdll.dll!RtlInitializeExceptionChain

REACHABLE LEAK 644 direct bytes 0x00534b78-0x00534dfc + 0 indirect bytes
# 0 ntdll.dll!RtlLookupAtomInAtomTable
# 1 ntdll.dll!RtlCreateAtomTable
# 2 KERNEL32.dll!GetThreadId 
# 3 KERNEL32.dll!LocalUnlock 
# 4 KERNEL32.dll!FindAtomA   
# 5 __cmshared_create_or_grab               [../../../../gcc-4.4.1/libgcc/../gcc/config/i386/cygming-shared-data.c:140]
# 6 __gcc_register_frame
# 7 register_frame_ctor
# 8 __do_global_ctors
# 9 __mingw_CRTStartup
#10 mainCRTStartup
#11 KERNEL32.dll!BaseThreadInitThunk

REACHABLE POSSIBLE LEAK 32 direct bytes 0x00534e28-0x00534e48 + 0 indirect bytes
# 0 ntdll.dll!RtlInitializeCriticalSectionEx    +0x12b    (0x77127de3 <ntdll.dll+0x57de3>)
# 1 ntdll.dll!RtlInitializeCriticalSectionEx    +0x82     (0x77127d3b <ntdll.dll+0x57d3b>)
# 2 ntdll.dll!RtlInitializeCriticalSection      +0x11     (0x7712f8d0 <ntdll.dll+0x5f8d0>)
# 3 ntdll.dll!RtlCreateAtomTable                +0x8b     (0x770f8767 <ntdll.dll+0x28767>)
# 4 ntdll.dll!RtlCreateAtomTable                +0x60     (0x770f873c <ntdll.dll+0x2873c>)
# 5 KERNEL32.dll!GetThreadId                    +0x1e     (0x7726a912 <KERNEL32.dll+0x4a912>)
# 6 KERNEL32.dll!LocalUnlock                    +0x44     (0x772692ea <KERNEL32.dll+0x492ea>)
# 7 KERNEL32.dll!FindAtomA                      +0x10     (0x7725bac1 <KERNEL32.dll+0x3bac1>)
# 8 __cmshared_create_or_grab                    [../../../../gcc-4.4.1/libgcc/../gcc/config/i386/cygming-shared-data.c:140]
# 9 __gcc_register_frame
#10 register_frame_ctor
#11 __do_global_ctors

REACHABLE LEAK 158 direct bytes 0x00534f30-0x00534fce + 0 indirect bytes
# 0 ntdll.dll!RtlLookupAtomInAtomTable       +0x1f1    (0x770f86af <ntdll.dll+0x286af>)
# 1 ntdll.dll!RtlAddAtomToAtomTable          +0xbd     (0x770f5e8e <ntdll.dll+0x25e8e>)
# 2 KERNEL32.dll!GetSystemDefaultLCID        +0x395    (0x77269bfd <KERNEL32.dll+0x49bfd>)
# 3 KERNEL32.dll!AddAtomA                    +0x10     (0x77259ec9 <KERNEL32.dll+0x39ec9>)
# 4 __cmshared_create_or_grab                 [../../../../gcc-4.4.1/libgcc/../gcc/config/i386/cygming-shared-data.c:118]
# 5 __gcc_register_frame
# 6 register_frame_ctor
# 7 __do_global_ctors
# 8 __mingw_CRTStartup
# 9 mainCRTStartup
#10 KERNEL32.dll!BaseThreadInitThunk
#11 ntdll.dll!RtlInitializeExceptionChain

DUPLICATE ERROR COUNTS:

SUPPRESSIONS USED:

NO ERRORS FOUND:
      0 unique,     0 total unaddressable access(es)
      0 unique,     0 total uninitialized access(es)
      0 unique,     0 total invalid heap argument(s)
      0 unique,     0 total warning(s)
      0 unique,     0 total,      0 byte(s) of leak(s)
      0 unique,     0 total,      0 byte(s) of possible leak(s)
ERRORS IGNORED:
     64 still-reachable allocation(s)
Details: C:\DrMemory/drmemory/logs/DrMemory-a.exe.7400.000/results.txt
4

2 回答 2

1

也许不是 Dr. Memory,但下面是用户忘记初始化数据的示例。我将这些描述为人们忘记初始化数据的规范方式。人们忘记了 malloc 不会清除内存区域。

struct object_t {
    int x;
    int *y;
};

struct object_t *object = malloc(sizeof(struct object_t));

if (object->y == NULL) {
    <Do stuff>
}

if (object->x == 32) {
    <Do stuff>
}
于 2012-03-12T15:29:46.500 回答
1

libc标准C 库main()( O 缓冲,将文件描述符连接到全局变量stdinstdout等等stderr

这段代码确实分配了内存,但它也应该释放任何资源,所以我也很难过为什么 DrMemory 会报告那里的泄漏。

一种理论可能是 GCC 在这种特殊情况下优化了代码(因为操作系统会在您的进程终止后清理任何此类问题),但我对此表示怀疑。为了验证它,打印一些东西到stdout- 这应该启用清理。如果内存泄漏消失,那就是优化器故障。

或者,由于所有泄漏都在本机 DLL 代码中,因此这是 Microsoft 的错误。

尝试以下步骤:

  1. 为您的操作系统安装所有服务包

  2. 向 DrMemory 报告错误 - 也许他们知道更多

  3. 尝试使用或不使用优化选项和不同级别的攻击性来编译更复杂的代码。

  4. 尝试不同版本的 GCC

于 2012-03-13T12:58:24.657 回答