2012-03-12 82 views
0

我刚刚下载了DrMemory,并希望检查我的应用程序中的内存泄漏情况,发现DrMemory在“空程序”中显示内存泄漏!GCC内存泄漏空程序?

  1. 在哪里这些内存泄漏是从哪里来的?
  2. 其次,究竟是什么样的例子初学者经常做错事导致DrMemory抱怨未初始化的访问(ES)

请注意,我是比较新的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

的如果没有标志,以抑制这些类型的报表,也许不是你的代码,但DrMemory这里失败了。 – 2012-03-12 15:22:57

回答

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

因此,*未初始化访问*意味着在实际设置之前提供数据?把'object-> x = 16; object-> y = 0;'之前会清除这样的错误呢? – 2012-03-12 15:32:49

+0

探查器是否可能不知道内存已被初始化?例如,如果它来自另一个上下文?例如当'* object'来自像'struct object_t * object_t_new();'这样的函数时。 – 2012-03-12 15:35:38

+1

@NiklasR:对于你的第一个问题,这是正确的。第二,这都是你的工具准确度的问题。如果它在虚拟机中运行程序(valgrind这样做),那么它可以给出非常好的答案。我不知道Dr.Memory是如何工作的,所以我无法真正给出答案。 – 2012-03-12 16:06:50

1

标准C库(libc)总是与你的代码,它包含了特殊的设置代码,例如,知道在哪里/如何OS将提供命令行参数,所以它可以设置该字符串数组调用main(),设置I/O缓存,文件描述符连接到全局stdinstdoutstderr

此代码分配内存,但它也应该释放所有的资源,所以我也难倒DrMemory为什么会在那里报告泄漏。

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

或者,由于所有的泄漏是在本机DLL的代码,这是微软的错误。

请尝试以下步骤:

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

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

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

  4. 尝试不同版本的GCC