native memory leak - how to find callstack of allo

2019-04-01 22:33发布

问题:

Based on following output of !address -summary command, I think I have got a native memory leak. In order to deterine the callstack on where these allocations are happening, I am following article at http://www.codeproject.com/KB/cpp/MemoryLeak.aspx

0:000> !address -summary  
 TEB 7efdd000 in range 7efdb000 7efde000  
 TEB 7efda000 in range 7efd8000 7efdb000  
 TEB 7efd7000 in range 7efd5000 7efd8000  
 TEB 7efaf000 in range 7efad000 7efb0000  
 TEB 7efac000 in range 7efaa000 7efad000  
 ProcessParametrs 00441b78 in range 00440000 00540000  
 Environment 004407f0 in range 00440000 00540000  

-------------------- Usage SUMMARY --------------------------  
    TotSize (      KB)   Pct(Tots) Pct(Busy)   Usage  
    551a000 (   87144) : 04.16%    14.59%    : RegionUsageIsVAD  
   5b8d3000 ( 1499980) : 71.53%    00.00%    : RegionUsageFree  
    2cc3000 (   45836) : 02.19%    07.68%    : RegionUsageImage  
     4ff000 (    5116) : 00.24%    00.86%    : RegionUsageStack  
          0 (       0) : 00.00%    00.00%    : RegionUsageTeb  
   1c040000 (  459008) : 21.89%    76.87%    : RegionUsageHeap  
          0 (       0) : 00.00%    00.00%    : RegionUsagePageHeap  
       1000 (       4) : 00.00%    00.00%    : RegionUsagePeb  
          0 (       0) : 00.00%    00.00%    : RegionUsageProcessParametrs  
          0 (       0) : 00.00%    00.00%    : RegionUsageEnvironmentBlock  
       Tot: 7fff0000 (2097088 KB) Busy: 2471d000 (597108 KB)  


0:000> !heap -s  
LFH Key                   : 0x7fdcf95f  
Termination on corruption : DISABLED  
  Heap     Flags   Reserv  Commit  Virt   Free  List   UCR  Virt  Lock  Fast   
                    (k)     (k)    (k)     (k) length      blocks cont. heap   
-----------------------------------------------------------------------------  
00440000 00000002  453568 436656 453568     62    54    32    0      0   LFH    
006b0000 00001002      64     16     64      4     2     1    0      0        
002b0000 00041002     256      4    256      2     1     1    0      0        
00620000 00001002      64     16     64      5     2     1    0      0        
00250000 00001002      64     16     64      4     2     1    0      0        
007d0000 00041002     256      4    256      0     1     1    0      0        
005c0000 00001002    1088    388   1088      7    17     2    0      0   LFH  
02070000 00041002     256      4    256      1     1     1    0      0        
02270000 00041002     256    144    256      0     1     1    0      0   LFH  
04e10000 00001002    3136   1764   3136    384    36     3    0      0   LFH  
    External fragmentation  21 % (36 free blocks)  
-----------------------------------------------------------------------------  

But when I run !heap -p –a command, I don’t get any callstack, just the following. Any ideas how to get callstack of allocations source?

0:000> !heap -p -a 0218e008  
    address 0218e008 found in  
    _HEAP @ 4e10000  
      HEAP_ENTRY Size Prev Flags    UserPtr UserSize - state  
        0218e000 001c 0000  [00]   0218e008    000d4 - (busy)  

回答1:

If you don't get a call stack from !heap -p -a
The reason can be that you have not used gflags correctly
Remeber to use correct name including .exe
Try to start it inteactivly and go to the image tab, might be easier
Try with page heap, that also gives call stack



回答2:

You should use deleaker. It's powerful tool for debuging.



回答3:

use valgrind for linux and deleaker for windows.



回答4:

I know nothing about Windows, but at least on Unix systems a debugger (like gdb on Linux) is useful to understand callstacks.

And you could also circumvent some of your issues by using e.g. Boehm's conservative garbage collector. On many systems you can also hunt memory leaks with the help of valgrind