简体   繁体   中英

GCC memory leaks on empty program?

I've just downloaded DrMemory and wanted to check for memory leaks in my application where I came across that DrMemory shows memory leaks on an "empty program"!

  1. Where do those memory leaks come from?
  2. And secondly, what is an example of what beginners often do wrong which causes DrMemory to complain about uninitialized access(es) ?

Note that I am relatively new to C and all that native stuff, so I don't understand most of the things DrMemory tells me here..

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

Perhaps not Dr. Memory, but an example of how users forget to initialize data is below. I'd describe these as the canonical way that people forget to initialize data. People forget that malloc does not clear the memory area.

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>
}

The standard C library ( libc ) is always linked to your code and it contains special setup code which, for example, knows where/how the OS will provide command line arguments, so it can set up the string array to call main() , set up I/O buffering, connect the file descriptors to the globals stdin , stdout and stderr , etc.

This code does allocate memory but it should also release any resources, so I'm also stumped why DrMemory would report leaks there.

One theory could be that GCC optimizes the code in this special case (because the OS will clean up any such problems after your process terminates) but I doubt that. To verify it, print something to stdout - that should enable the cleanup. If the memory leaks go away, it was an optimizer glitch.

Or, since all the leaks are in native DLLs code, it's a bug by Microsoft.

Try these steps:

  1. Install all the service packs for your OS

  2. Report an error to DrMemory - maybe they know more

  3. Try to compile more complex code with and without optimization options and different levels of aggressiveness.

  4. Try different versions of GCC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM