[C++/MSXML] Memory leak in oggetti COM (IXMLDOMDocument)

the_tube

Nuovo Utente
21 Nov 2008
2
0
0
Ciao a tutti,

è un po' che sto ci sbattendo la testa contro ma non ne vengo fuori...

Devo creare un web service sotto WinXP, scritto in C++ con Visual Studio 2005 (SP1). Questo web service deve leggere ripetutamente gli XML di richiesta. Per far questo ho usato gli oggetti COM (MSXML), soltanto che ogni volta che leggo un XML, perdo memoria per strada, che non riesco a deallocare in nessuna maniera; ho provato anche con gli Smart Pointers, ma nulla, la quantità di memoria persa è la stessa che rilevo usando i metodi tradizionali.

A questo punto non so più se continuo a sbagliare qualcosa io, oppure si tratta di un memory leak dell'oggetto COM di MicroSoft... però penso che avrei trovato altri articoli che ne parlano... ed invece nulla.

All'uopo, ho creato una piccola applicazione di test che evidenzia questo comportamento. L'XML di test usato è il seguente:

Codice:
<RootNode>
	<NodeA value="theValueA" />
	<NodeB value="theValueB" />
	<NodeC value="theValueC" />
</RootNode>

Posto il codice di test, fiducioso... Grazie a tutti un anticipo!

TT.

Codice:
// INCLUDE
#include <atlbase.h>     // Includes CComVariant and CComBSTR.
#include <comdef.h>      // per _bstr_t
#include <objbase.h>     // per CoFreeUnusedLibrariesEx
#include <windows.h>
#include <string>

// DEFINE
#define RIUSCITO(hr) ((HRESULT)(hr) == S_OK)
#define FALLITO(hr)  ((HRESULT)(hr) != S_OK)
#define NUMBER_OF_ELABORATIONS        100000

// XMLREADER
void XMLReader()
{
    bool bErr = false;		// true se qualcosa è andato storto

    char pcXMLReq[1024] = "<RootNode><NodeA value=\"theValueA\" /><NodeB value=\"theValueB\" /><NodeC value=\"theValueC\" /></RootNode>";    
    
    // stringhe in cui memorizzerò i valori dei tre nodi
    std::string sNodeA = "";	
    std::string sNodeB = "";
    std::string sNodeC = "";

    // variabili di lettura dell'XML
    IXMLDOMDocument        *pXMLReq        = NULL;
    IXMLDOMNode            *pNode          = NULL;
    IXMLDOMNode            *pAttr          = NULL;
    IXMLDOMNamedNodeMap    *pAttributes    = NULL;
    CComVariant            value;

    HRESULT                hr;
    
    CoInitialize( NULL );

    printf( "Premi un tasto per iniziare il ciclo di %d elaborazioni\n", NUMBER_OF_ELABORATIONS );
    system( "pause" );

    for( int iCount=0; iCount<NUMBER_OF_ELABORATIONS; iCount++ )
    {
        // inizializzazione dell'XML
        if ( !bErr )
        {
            hr = CoCreateInstance( __uuidof(DOMDocument), NULL, 
                                   CLSCTX_INPROC_SERVER, __uuidof(IXMLDOMDocument), (void**)&pXMLReq );
            if( FALLITO( hr ) )
                bErr = true;
        }

        // caricamento dell'XML
        if ( !bErr )
        {
            VARIANT_BOOL load_result;
            if ( RIUSCITO( hr ) )    hr = pXMLReq->loadXML( _bstr_t( pcXMLReq ), &load_result );

            if( FALLITO( hr ) )
                bErr = true;
        }

        if ( !bErr )
        {
            // lettura e validazione dell'XML
            if ( RIUSCITO( hr ) )    hr = pXMLReq->selectSingleNode( L"RootNode/NodeA", &pNode );
            if ( RIUSCITO( hr ) )    hr = pNode->get_attributes( &pAttributes );
            if ( RIUSCITO( hr ) )    hr = pAttributes->getNamedItem(L"value",  &pAttr );
            if ( RIUSCITO( hr ) )    hr = pAttr->get_nodeValue( &value );    
            if ( RIUSCITO( hr ) )    sNodeA = std::string( _bstr_t( value ) );

            if ( RIUSCITO( hr ) )    hr = pXMLReq->selectSingleNode( L"RootNode/NodeB", &pNode );
            if ( RIUSCITO( hr ) )    hr = pNode->get_attributes( &pAttributes );
            if ( RIUSCITO( hr ) )    hr = pAttributes->getNamedItem(L"value",  &pAttr );
            if ( RIUSCITO( hr ) )    hr = pAttr->get_nodeValue( &value );
            if ( RIUSCITO( hr ) )    sNodeB = std::string( _bstr_t( value ) );

            if ( RIUSCITO( hr ) )    hr = pXMLReq->selectSingleNode( L"RootNode/NodeC", &pNode );
            if ( RIUSCITO( hr ) )    hr = pNode->get_attributes( &pAttributes );
            if ( RIUSCITO( hr ) )    hr = pAttributes->getNamedItem(L"value",  &pAttr );
            if ( RIUSCITO( hr ) )    hr = pAttr->get_nodeValue( &value );
            if ( RIUSCITO( hr ) )    sNodeC = std::string( _bstr_t( value ) );

            if( FALLITO( hr ) )
                bErr = true;
        }

        // rilascio delle risorse degli oggetti COM
        if ( pXMLReq )        
            pXMLReq->Release();
        if ( pNode )        
            pNode->Release();
        if ( pAttr)            
            pAttr->Release();
        if ( pAttributes )    
            pAttributes->Release();

        // status
	if ( ( iCount % 1000 ) == 0 )
	    printf( "%09d elaborazioni eseguite\n", iCount );
    }

    printf( "Fine del ciclo\n", NUMBER_OF_ELABORATIONS );
    system( "pause" );

    CoUninitialize();
}


int _tmain(int argc, _TCHAR* argv[])
{
	XMLReader();
	return 0;
}
 

Discussioni simili