msxml6,MRE中的内存泄漏

时间:2019-07-18 14:14:18

标签: memory-leaks com atl msxml msxml6

MRE:

#include <Windows.h>
#include <MsXml2.h>
#include <atlcomcli.h>

#include <fstream>

int main() {

    std::string strFileName( "xml.xml" );
    std::ofstream outFile( strFileName );
    outFile << "<root><n1><n2><n3><n4><n5/></n4></n3></n2></n1></root>";
    outFile.close();

    while ( CoInitializeEx( nullptr, COINIT_APARTMENTTHREADED ) == S_OK ) { 
        {
            CComPtr<IXMLDOMDocument2> spIXMLDOMDocument2;
            HRESULT hr = spIXMLDOMDocument2.CoCreateInstance( L"Msxml2.DOMDocument.6.0", nullptr, CLSCTX_INPROC_SERVER );
            if ( hr == S_OK ) {
                VARIANT_BOOL vbSuccess = VARIANT_FALSE;
                CComVariant ccvarXmlSource( strFileName.c_str() );
                hr = spIXMLDOMDocument2->load( ccvarXmlSource, &vbSuccess );
            } else exit( 0 );
        }
        CoUninitialize();
        Sleep( 0 );
    }

}

代码只是CoInitializeEx CoCreateInstance load ReleaseCoUninitialize的无限循环(在CComPtr的帮助下)< / p>

发布版本,x64,Visual Studio 2017,Windows 10(1809)。 在我的笔记本电脑上,每分钟泄漏数十KB。 开始时,Process Explorer向我显示了2,5Mo的专用字节,然后3小时后,则是19Mo。

C:\ Windows \ System32 \ msxml6.dll版本:6.30.17763.437

更新:线程MRE

#include <Windows.h>
#include <MsXml2.h>
#include <atlcomcli.h>

#include <fstream>
#include <thread>

void ThreadFunction( ) {

        if ( CoInitializeEx( nullptr, COINIT_APARTMENTTHREADED ) != S_OK ) exit ( 0 );
        {
            CComPtr<IXMLDOMDocument2> spIXMLDOMDocument2;
            HRESULT hr = spIXMLDOMDocument2.CoCreateInstance( L"Msxml2.DOMDocument.6.0", nullptr, CLSCTX_INPROC_SERVER );
            if ( hr == S_OK ) {
                VARIANT_BOOL vbSuccess = VARIANT_FALSE;
                CComVariant ccvarXmlSource( "xml.xml" );
                hr = spIXMLDOMDocument2->load( ccvarXmlSource, &vbSuccess );
            } else exit( 0 );
        }
        CoUninitialize();

}

int main() {

    std::ofstream outFile( "xml.xml" );
    outFile << "<root><n1><n2><n3><n4><n5/></n4></n3></n2></n1></root>";
    outFile.close();

    while ( true ) {

        std::thread oneThread( ThreadFunction );
        oneThread.join();

        Sleep( 0 );

    }

}

可以复制吗?

0 个答案:

没有答案