Debug Labs


Memory Leak in Component

This lab will demonstrates memory leak in an COM Component. It uses the tool "LeakDiag" to find the culprit

 

1- Lab Setup
	

Note: This lab assumes that you have already installed the necessary lab files to run it.

1- Verify the site is running in medium protection

2- Verify that the process "dllhost.exe" mapped to the out of process pooled application is running and note down its PID

To find the PID of the desired dllhost, do the follwing:

- Open Component Services

- Expand "Component Services" Node

- Expand "Computers" Node

- Expand "My Computer" Node

- Select "COM+ Applications"

- Click "Status View" icon and notice the PID column on the right hand pane

 

3- Run Performace Monitor to monitor the process memory allocations.

a- Start Performance Monitor

b- Click the Add button

c- Under "Performance Object", Select "Process"

d- Under "Select Counters from List", select "Private Bytes" and "Virtual Bytes"

e- Under "Select Instances from List", select "dllhost#1"

3- Attach LeakDiag tool to the dllhost process for the Out-of-Process pooled applications

4- Add the symbol path in LeakDiag.

a) Using the GUI:

- Click "File"

- Click "Symbol File Path"

- Type in SRV*C:\LabSymbols*\\symbols\symbols;C:\iisdebuglabs\symbols\Pri and hit Enter

b) Choose the allocator to track

5- Choose the allocator to track 

Note: For more information on the LeakDiag tool, please visit http://leakdiag.

6- In LeakDiag GUI, click "Start"

 

 

2- Run Scenario

1- Depending on the allocator you chose to track in LeakDiag, Click the eaquivalent button to allocate each time 1024 B.
 For Example if you are tracking The C-Runtime allocator 
	a- Click "Allocate 1024 B (malloc)" many times. 	    
b- In "LeakDiag" GUI, generate a log file.
c- In "Performance Monitor", Compare the counters "Private Bytes" and "Virtual Bytes"

d- Repeat steps a and b four or five times
2- After taking the last log in LeakDiag, right click the process "dllhost" and choose "Genrate Dump" to generate a user dump

                

1- Debug Annotation

 

1) What is memory leak?

 

Memory Leaks show up as an increasing Private Virtual size. At some point, the memory manager will stop the process from growing in physical size, though it can continue to

grow in its virtual size until it reaches around 1.5 GB.

2) Tracking "Windows Heap Allocator"

 

1- LeakDiag Logs without symbols

 

Using LDParser that you can download from http://leakdiag to read the logs, open one of the logs and make sure the "Stacks" table is sorted on "totalsize" column.

Select the first row in the "Stacks" table if you sorted on "totalsise" decreasing.

 

numallocs STACKID size totalsize

69 0761CA88 1024 70656

 

 

num dll function offset filename line addr

0 MSVCRT.dll _heap_alloc 0xED 0x780010A8

1 MSVCRT.dll _nh_malloc 0x10 0x78001045

2 OLEAUT32.dll DispCallFunc 0x15D 0x779D7B9D

3 OLEAUT32.dll CTypeInfo2__Invoke 0x244 0x77A22C42

4 badcom.dll DllUnregisterServer 0x853 0x6E625D0  

 

 

This shows that the stack above did 69 allocations of 1024 B, a total of 70656 B and that "badCom.dll" is the one that made these allocations.

For precise reading of leakDiag logs, it is important that you compare the totalsize of allocations of the suspected stack to the increase you see in

performance monitor in "Private Bytes".

 

This data is enough to conclude that the culprit here is "badCom.dll"

 

2- LeakDiag Logs with symbols

 

num dll function offset filename line addr

0 MSVCRT.dll _heap_alloc 0xED 0x780010A8

1 MSVCRT.dll _nh_malloc 0x10 0x78001045

2 OLEAUT32.dll tPushValJmpTab+0xf5 0x15D 0x779D7B9D

3 OLEAUT32.dll CTypeInfo2__Invoke 0x244 0x77A22C42

4 badcom.dll doMemLeak1 0x30 0x6E625D0 

 

 

numallocs STACKID size totalsize

69 0761CA88 1024 70656

 

Back in Windbg, open the dump file generated by leakDiag to double check findings:

 

The addresses that LeakDiag logs show in the stack are where the functions are loaded in memory.

The "ln" debugger command shows what module!function is loaded in the specified address.

 

 

0:000> ln 0x6E625D0

(06e6258f) badCOM!doMemLeak1+0x30

 

You could use these addresses as well to walk through the disassembly for in depth analysis.

   

3) Tracking "C Runtime Allocator"

 

1- LeakDiag Logs without symbols

 

Open a leakDiag log, make sure that the table "STACKS" is sorted, select the row that corresponds to the highest "totalsize"

 

numallocs STACKID size totalsize

151 07164C48 1024 154624

 

The correspoding frames would look like:

 

num dll function offset filename line addr

0 badcom.dll doMemLeak1 0x24 0x6E6285B

1 OLEAUT32.dll DispCallFunc 0x15D 0x779D7B9D

2 OLEAUT32.dll CTypeInfo2__Invoke 0x244 0x77A22C42

3 badcom.dll DllUnregisterServer 0x853 0x6E625D0

4 vbscript.dll CatchIDispatchInvoke 0x9E 0x6B61DFF3

 

 

This shows that the stack above did 151 allocations of 1024 each, a total of 154624 B allocated. This is the highest total allocations size in this log which leaves no

doubts that the culprit is "badCom.dll".

 

2- LeakDiag Logs with symbols

 

 

numallocs STACKID size totalsize

151 07164C48 1024 154624

 

The correspoding frames would look like:

 

num dll function offset filename line addr

0 badcom.dll doMemLeak1 0x36 0x6E6285B

1 OLEAUT32.dll tPushValJmpTab 0x120 0x779D7B9D

2 OLEAUT32.dll CTypeInfo2__Invoke 0x244 0x77A22C42

3 badcom.dll ATL__IDispatchImpl 0x853 0x6E625D0

4 vbscript.dll CatchIDispatchInvoke 0x9E 0x6B61DFF3

 

 

 

This shows that the function "doMemLeak1" in the module "badCom.dll" is responsible for these allocations. This is enough

to tell the customer to review the code of this function for resolution.

 

 

4) Tracking "Virtual Memory Allocator"

 

Open a leakDiag log generated while tracking "Virtual Memory Allocator", make sure that the table "STACKS" is sorted, select the row that corresponds to the highest "totalsize"

 

numallocs STACKID size totalsize

348 07164008 4096 1425408

 

The correspoding frames would look like:

 

num dll function offset filename line addr

0 KERNEL32.DLL VirtualAllocEx 0x62 0x77E87FC7

1 KERNEL32.DLL VirtualAlloc 0x16 0x77E87F66

2 badcom.dll DllUnregisterServer 0x1242 0x6E62FBF

3 badcom.dll DllUnregisterServer 0xCD9 0x6E62A56

4 OLEAUT32.dll DispCallFunc 0x15D 0x779D7B9D

 

 

This shows that the stack above did 256 allocations of 1024 each, a total of 1048576 B allocated. This is the highest total allocations size in this log which leaves no

doubts that the culprit is "badext.dll".

 

2- LeakDiag Logs with symbols

 

numallocs STACKID size totalsize

348 07164008 4096 1425408

 

The correspoding frames would look like:

 

num dll function offset filename line addr

0 KERNEL32.DLL VirtualAllocEx 0x62 0x77E87FC7

1 KERNEL32.DLL VirtualAlloc 0x16 0x77E87F66

2 badCom.dll doMemLeak1 0x49 0x21E184B

3 badCom.dll CScenarios__processSc 0x20C 0x21E14EA

4 OLEAUT32.dll tPushValJmpTab 0xf5 0x779D7B9D

 

 

With symbols, we can see that the function "doMemLeak1()" is the one calling the win32 API "virtualAlloc()" to allocate virtual memory.

Again, this is enough to confirm that the culprit is "badext.dll".

 

Here is how the function "doMemLeak" is implemented.

 

DWORD doMemLeak1(lpEcb, int Variation)

{

char *szVar = new char[10];

char *szVal = new char[10];

 

getQSVars (lpEcb, szVar, szVal);

int iType;

iType = atoi( szVal );

 

if (1 == Variation)

{

void* lp = NULL;

switch ( iType )

{

case 1: lp = HeapAlloc( GetProcessHeap(), 0, 1024 ); //tracked by "WIndows Heap Allocator"

break;

case 2: lp = malloc( 1024 ); //Tracked by "C Runtime Allocator"

break;

case 3: lp = VirtualAlloc( NULL, 1024, MEM_COMMIT, PAGE_READWRITE ); //Tracked by "Virtual Memory Allocator"

break;

default: lp = HeapAlloc( GetProcessHeap(), 0, 1024 ); break;

}

}

return HSE_STATUS_SUCCESS;

}

 

 

 


- Microsoft Confidential -