空程序中GCC内存泄漏?

时间:2012-03-12 15:18:16

标签: c gcc memory-leaks dr-memory

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

  1. 那些内存泄漏来自哪里?
  2. 其次,什么是初学者经常做错的一个例子,导致DrMemory抱怨未初始化的访问
  3. 请注意,我对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
    

2 个答案:

答案 0 :(得分:1)

也许不是记忆博士,但下面是用户忘记初始化数据的示例。我将这些描述为人们忘记初始化数据的规范方式。人们忘记了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>
}

答案 1 :(得分:1)

标准C库(libc)始终链接到您的代码,它包含特殊的设置代码,例如,知道操作系统将在何处/如何提供命令行参数,因此它可以设置字符串要调用main()的数组,设置I / O缓冲,将文件描述符连接到全局stdinstdoutstderr等。

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

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

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

尝试以下步骤:

  1. 安装操作系统的所有Service Pack

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

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

  4. 尝试不同版本的GCC