-
MSXML driving me insane!
Code:
#import <msxml6.dll>
int main()
{
::CoInitialize(NULL);
MSXML2::IXMLDOMDocumentPtr doc;
doc.CreateInstance(__uuidof(MSXML2::DOMDocument));
doc->validateOnParse = true;
if (doc->load("c:\\test.xml"))
{
}
::CoUninitialize();
}
i'm getting a memory access violation in comip.h at the following code:
Code:
void _Release() throw()
{
if (m_pInterface != NULL) {
m_pInterface->Release();
}
}
the test.xml is a valid XML file. is there some funky magic happening in the background i should know about? thanks.
-
i did more digging and now i'm very confused....haha...
both of the following versions will not throw the memory access violation anymore.
version A:
Code:
int main()
{
::CoInitialize(NULL);
MSXML2::IXMLDOMDocumentPtr doc;
doc.CreateInstance(__uuidof(MSXML2::DOMDocument));
doc.Release();
::CoUninitialize();
}
version B:
Code:
class CXmlDocument
{
private:
MSXML2::IXMLDOMDocumentPtr mp_Document;
public:
CXmlDocument()
{
::CoInitialize(NULL);
mp_Document.CreateInstance(__uuidof(MSXML2::DOMDocument));
}
~CXmlDocument()
{
mp_Document = 0;
::CoUninitialize();
}
MSXML2::IXMLDOMDocumentPtr operator->() { return mp_Document; }
};
int main()
{
CXmlDocument doc;
}
if i use Release() in CXmlDocument's destructor, i will get a memory access violation. can someone please explain what's happening? i just want to make sure i understand so there's no memory leaks due to my ignorance. thanks.
-
>> i'm getting a memory access violation
The object "doc" is going out of scope (invoking destructors) after ::CoUninitialize(). Version A and B are working because you are calling Release() prior to ::CoUninitialize().
>> if i use Release() in CXmlDocument's destructor, i will get a memory access violation
"mp_Document = 0" is the same as calling Release(). Just do it while the COM sub-system is still running :)
gg
-
ok, it makes more sense now.
since _com_ptr_t is a smart pointer, i shouldn't need to call the Release() function at any time right? and the problems i'm having are because the Release(), whether manually or automatically, are called after CoUninitialize?
-
>> i shouldn't need to call the Release() function at any time right?
You can rely on _com_ptr_t's destructor if you like - yes.
>> and the problems i'm having are because the Release() ... called after CoUninitialize?
That's definitely the problem with post #1.
gg