Reputation: 6478
I'm using MSXML 4.0 for parsing my xml.
The program works fine, but just before the program terminates I get the following exception
Unhandled exception at 0x00417be5 in db2.exe: 0xC0000005: Access violation reading location 0x00b1c740.
at comip.h
at _Release()
method at line m_pInterface->Release();
Here's my code
#include <iostream>
#include "stdafx.h"
#include "Database.h"
#include <sstream>
#include <objbase.h>
#import <msxml4.dll>
using namespace std;
void main(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL);
MSXML2::IXMLDOMDocumentPtr pXMLDom;
HRESULT hr= pXMLDom.CreateInstance(__uuidof(MSXML2::DOMDocument40), NULL, CLSCTX_INPROC_SERVER); //Msxml2.DOMDocument.4.0
if (FAILED(hr))
{
printf("Failed to instantiate an XML DOM.\n");
//return 0;
}
try
{
pXMLDom->async = VARIANT_FALSE;
pXMLDom->validateOnParse = VARIANT_FALSE;
pXMLDom->resolveExternals = VARIANT_FALSE;
if(pXMLDom->load("users.xml") == VARIANT_TRUE)
{
printf("XML DOM loaded from users.xml:\n%s\n", (LPCSTR)pXMLDom->xml);
}
else
{
// Failed to load xml
printf("Failed to load DOM from stocks.xml. %s\n",
(LPCSTR)pXMLDom->parseError->Getreason());
}
::CoUninitialize();
}
catch(_com_error errorObject)
{
printf("Exception thrown, HRESULT: 0x%08x", errorObject.Error());
}
catch(...)
{
printf("Unknown error");
}
}
I don't know what is wrong with my program. It just throws an exception before the last line.
Can someone point me in a right direction?
Edit : Strange I removed ::CoUninitialize();
from my program and it works fine.
What is wrong with ::CoUninitialize();
method ?
Thanks,
Upvotes: 3
Views: 3276
Reputation: 18507
You are implicitly using COM after you have called CoUninitialize
. This is done in the destructor of MSXML2::IXMLDOMDocumentPtr
, which calls the function IXMLDOMDocument::Release
.
CoUninitialize
must be the absolute last COM function ever been called.
Make these changes, and it will work. I've made sure that CoUninitialize
will be called after the destructor of MSXML2::IXMLDOMDocumentPtr
:
void XMLDomTest()
{
MSXML2::IXMLDOMDocumentPtr pXMLDom;
HRESULT hr= pXMLDom.CreateInstance(__uuidof(MSXML2::DOMDocument40), NULL, CLSCTX_INPROC_SERVER); //Msxml2.DOMDocument.4.0
if (FAILED(hr))
{
printf("Failed to instantiate an XML DOM.\n");
return;
}
try
{
pXMLDom->async = VARIANT_FALSE;
pXMLDom->validateOnParse = VARIANT_FALSE;
pXMLDom->resolveExternals = VARIANT_FALSE;
if(pXMLDom->load("users.xml") == VARIANT_TRUE)
{
printf("XML DOM loaded from users.xml:\n%s\n", (LPCSTR)pXMLDom->xml);
}
else
{
// Failed to load xml
printf("Failed to load DOM from stocks.xml. %s\n",
(LPCSTR)pXMLDom->parseError->Getreason());
}
}
catch(_com_error errorObject)
{
printf("Exception thrown, HRESULT: 0x%08x", errorObject.Error());
}
catch(...)
{
printf("Unknown error");
}
}
void main(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL);
XMLDomTest();
::CoUninitialize();
}
Upvotes: 5