【问题标题】:Enumerating printer folder via IShellFolder interface causes heap allocation leak通过 IShellFolder 接口枚举打印机文件夹导致堆分配泄漏
【发布时间】:2016-01-03 07:57:48
【问题描述】:

我需要为 VB6 应用程序提供一个函数,该函数枚举当前用户的打印机。由于列出了网络上的所有打印机,内置 VB6 打印机对象在终端服务器上失败。

在 Win7 x64 SP1 上使用 VC2013 Update 5 编译。注意:省略错误检查

#include <Windows.h>
#include <ShlObj.h>
#pragma comment(lib, "Shell32.lib")

int main(int argc, wchar_t* argv[])
{
    HRESULT hr = CoInitialize(0);   

    ULONG ulFetched = 0;
    LPITEMIDLIST pPidl = NULL;
    IShellFolder *pPrinterFolder = NULL;
    IEnumIDList *pEnum = NULL;  
    IShellFolder *pDesktopFolder = NULL;

    hr = SHGetDesktopFolder(&pDesktopFolder);   

    LPITEMIDLIST pPidlLocation = NULL;
    hr = SHGetSpecialFolderLocation(NULL, CSIDL_PRINTERS, &pPidlLocation);

    hr = pDesktopFolder->BindToObject(pPidlLocation, NULL, IID_IShellFolder, (void **)&pPrinterFolder);

    hr = pPrinterFolder->EnumObjects(NULL, SHCONTF_NONFOLDERS, &pEnum);

    while ((hr = pEnum->Next(1, &pPidl, &ulFetched)) == S_OK && ulFetched > 0)
    {
        // Do something with item
        CoTaskMemFree(pPidl);
    }

    CoTaskMemFree(pPidlLocation);

    pEnum->Release();
    pPrinterFolder->Release();
    pDesktopFolder->Release();

    // Heap allocation leak
    CoUninitialize();

    return 0;       
}

问题是调用CoUnitialize() 会在应用程序验证器监控时导致堆分配泄漏:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<avrf:logfile xmlns:avrf="Application Verifier">
    <avrf:logSession TimeStarted="2015-10-06 : 13:13:37" PID="1880" Version="2">
        <avrf:logEntry Time="2015-10-06 : 13:13:40" LayerName="Leak" StopCode="0x900" Severity="Error">
            <avrf:message>A heap allocation was leaked.</avrf:message>
            <avrf:parameter1>6ff7ff0 - Address of the leaked allocation. Run !heap -p -a &lt;address&gt; to get additional information about the allocation.</avrf:parameter1>
            <avrf:parameter2>49a5774 - Address to the allocation stack trace. Run dps &lt;address&gt; to view the allocation stack.</avrf:parameter2>
            <avrf:parameter3>5bd8fe8 - Address of the owner dll name. Run du &lt;address&gt; to read the dll name.</avrf:parameter3>
            <avrf:parameter4>11390000 - Base of the owner dll. Run .reload &lt;dll_name&gt; = &lt;address&gt; to reload the owner dll. Use &apos;lm&apos; to get more information about the loaded and unloaded modules.</avrf:parameter4>
            <avrf:stackTrace>
                <avrf:trace>vfbasics!+59d8a7b7 ( @ 0)</avrf:trace>
                <avrf:trace>vfbasics!+59d8b031 ( @ 0)</avrf:trace>
                <avrf:trace>vfbasics!+59d86ac4 ( @ 0)</avrf:trace>
                <avrf:trace>ntdll!RtlApplicationVerifierStop+1a6 ( @ 0)</avrf:trace>
                <avrf:trace>ntdll!RtlUlonglongByteSwap+222e ( @ 0)</avrf:trace>
                <avrf:trace>ntdll!LdrUnloadDll+4a ( @ 0)</avrf:trace>
                <avrf:trace>vfbasics!+59d87065 ( @ 0)</avrf:trace>
                <avrf:trace>KERNELBASE!FreeLibrary+15 ( @ 0)</avrf:trace>
                <avrf:trace>ole32!PropVariantCopy+746 ( @ 0)</avrf:trace>
                <avrf:trace>ole32!PropVariantCopy+81c ( @ 0)</avrf:trace>
                <avrf:trace>ole32!PropVariantCopy+830 ( @ 0)</avrf:trace>
                <avrf:trace>ole32!PropVariantCopy+7b7 ( @ 0)</avrf:trace>
                <avrf:trace>ole32!SetErrorInfo+75 ( @ 0)</avrf:trace>
                <avrf:trace>vfbasics!+59d8ee93 ( @ 0)</avrf:trace>
                <avrf:trace>userprinters!main+183 (c:\projects\userprinters\userprinters\userprinters.cpp @ 44)</avrf:trace>
                <avrf:trace>userprinters!__tmainCRTStartup+199 (f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c @ 626)</avrf:trace>
                <avrf:trace>userprinters!mainCRTStartup+d (f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c @ 466)</avrf:trace>
                <avrf:trace>kernel32!BaseThreadInitThunk+12 ( @ 0)</avrf:trace>
                <avrf:trace>ntdll!RtlInitializeExceptionChain+63 ( @ 0)</avrf:trace>
                <avrf:trace>ntdll!RtlInitializeExceptionChain+36 ( @ 0)</avrf:trace>
            </avrf:stackTrace>
        </avrf:logEntry>
    </avrf:logSession>
</avrf:logfile>

附带说明,CoInitialize() 似乎是最新 Windows 版本的要求,如 here 所述

谁能指出导致此泄漏的正确方向?

【问题讨论】:

  • 很可能是来自 App Verifier 的误报 - social.msdn.microsoft.com/Forums/en-US/…
  • 顺便说一句,你也可以在 VB6 中实现这个枚举 - stackoverflow.com/questions/1063874/…
  • 只要所有 COM 调用都成功(因为您没有进行任何错误处理),显示的代码中没有泄漏。顺便说一句,如果IEnumIDList::Next() 返回S_OK,它永远不会将ulFetched 设置为0。由于您一次请求 1 个 PIDL,它只能返回 ulFetched,你可以在用celt=1调用Next()时设置pceltFetched=NULL。文档甚至这么说。

标签: c++ winapi memory-leaks com vb6


【解决方案1】:

是的,处理打印机枚举的模块 prncache.dll 和 prhfldr.dll 中似乎存在泄漏。

首先我修改了你的代码,添加了一个循环来检查任务管理器中的内存使用情况:

while (true)
{
    HRESULT hr = CoInitialize(0);

    ULONG ulFetched = 0;
    LPITEMIDLIST pPidl = NULL;
    IShellFolder *pPrinterFolder = NULL;
    IEnumIDList *pEnum = NULL;
    IShellFolder *pDesktopFolder = NULL;

    hr = SHGetDesktopFolder(&pDesktopFolder);

    LPITEMIDLIST pPidlLocation = NULL;
    hr = SHGetSpecialFolderLocation(NULL, CSIDL_PRINTERS, &pPidlLocation);

    hr = pDesktopFolder->BindToObject(pPidlLocation, NULL, IID_IShellFolder, (void **)&pPrinterFolder);

    hr = pPrinterFolder->EnumObjects(NULL, SHCONTF_NONFOLDERS, &pEnum);

    while ((hr = pEnum->Next(1, &pPidl, &ulFetched)) == S_OK && ulFetched > 0)
    {
        // Do something with item
        CoTaskMemFree(pPidl);
    }

    CoTaskMemFree(pPidlLocation);

    pEnum->Release();
    pPrinterFolder->Release();
    pDesktopFolder->Release();

    // Heap allocation leak
    CoUninitialize();
}

我看到内存使用量正在增长。

然后我取了 Deleaker,用 CoInitialize() 在该行设置断点,并制作了几个快照,以便稍后进行比较。

这是我得到的:

【讨论】:

  • 感谢您调查此问题。因此,似乎其中一个 DLL 确实存在泄漏。它可能不会造成任何大问题,但泄漏内存仍然感觉不对。
  • 定义泄漏。这些 dll 在设计上可能永远不会被 shell 加载,因此释放内存的代码是无关紧要的。
  • @Aurora 作为一种解决方法,我将创建一个单独的应用程序,该应用程序将枚举并将结果返回给主应用程序。
  • @ArtemRazin 是的,这可能是处理泄漏的最简单方法。
猜你喜欢
  • 2019-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-30
  • 2019-08-12
  • 2020-02-18
相关资源
最近更新 更多