The Windows Embedded CE 6.0 R3 AppVerifier tool may report a memory leak in CoInitializeEx


Symptoms


You are using the AppVerifier tool with the Heap Verifier (Shim_heap.dll) shim to test for memory leaks in your application. The applications that you are testing calls CoInitializeEx to initialize the COM. Although your application calls CoUninitialize to close the COM library and free any resources that it maintains, the AppVerifier tool reports one un-freed item and ole32.dll!CoInitializeEx is located on the call stack of that report.

Cause


The problem is caused by the design of the ole32.dll DLL_PROCESS_DETACH handling in Windows Embedded CE 6.0 R3 which triggers a known false-positive memory leak being reported by AppVerifier. When DLL_PROCESS_DETACH is processed, a call to TlsFree is intentionally skipped because the kernel does automatically clean up the TLS slot shortly after the DllMain function in ole32.dll returns.

Resolution


This AppVerifier false-positive can safely be ignored because the kernel does automatically clean up the relevant TLS slot.

The following is the relevant excerpt of the debug output from the ARMV4I Device Emulator image dealing with the memory leak the Heap Verifier shim (Shim_heap.dll) reports when I tested a simple sample application (repro_app.exe) that just executes the following code:

Sample code:
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (hr == S_OK || hr == S_FALSE)
CoUninitialize();
Relevant debug output:
DLL_PROCESS_DETACH: Shim_heap checking for abandoned heaps/allocations...
Heap statistics for heap 0x00030000:
Initial size: 0, Maximum size: 0
Current bytes: 108, Max: 230
Current count: 1, Max: 6
Free list bytes: 122
Free list size: 0
Checking for un-freed items in heap 0x00030000...
----------------------------------------
TrackedItem count: 1
Callstack:
Unable to find .map for 'shim_heap.dll' (GLE: 2). Function names will not be available.
0x41778310: shim_heap.dll!(null) + 8310h
0x40506c80: ole32.dll!?TLSAllocData@COleTls@@AAAJXZ + 18h
0x404e9adc: ole32.dll!??0COleTls@@QAA@AAJ@Z + 5ch
0x404ea1ec: ole32.dll!CoInitializeEx + 30h
Unable to find .map for 'repro_app.exe' (GLE: 2). Function names will not be available.
0x00011024: repro_app.exe!(null) + 11024h
0x000113e0: repro_app.exe!(null) + 113e0h
0x00011458: repro_app.exe!(null) + 11458h
0x4003bc2c: coredll.dll!MainThreadBaseFunc + 194h
Un-freed items:
0x00032b60 108 bytes, process: 0x023c0036, thread: 0x023d0036, PerfCount: 3104481198
Total: 108 bytes