MSXML driving me insane!

This is a discussion on MSXML driving me insane! within the Windows Programming forums, part of the Platform Specific Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    188

    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.

  2. #2
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    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.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    >> 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

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    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?

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    >> 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Driving me insane - Debugger
    By Hugo716 in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2006, 11:09 AM
  2. Conceptually simple & driving me insane
    By cboard_member in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2006, 12:35 PM
  3. JAVA issue driving me insane
    By axon in forum Tech Board
    Replies: 0
    Last Post: 02-23-2004, 08:19 PM
  4. VC++ tutorial driving me insane!
    By Swaine777 in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 07:35 AM
  5. Linked Lists - Driving me insane
    By Steven Snell in forum C Programming
    Replies: 3
    Last Post: 10-21-2002, 09:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21