All,
this is my code
//declare string pointer BSTR markup; //initialize markup to some well formed XML <- //declare and initialize XML Document MSXML2::IXMLDOMDocument2Ptr pXMLDoc; HRESULT hr; hr = pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40)); pXMLDoc->async = VARIANT_FALSE; pXMLDoc->validateOnParse = VARIANT_TRUE; pXMLDoc->preserveWhiteSpace = VARIANT_TRUE; //load markup into XML document vtBoolResult = pXMLDoc->loadXML(markup); //do some changes to the XML file<- //get back string from XML doc markup = pXMLDoc->Getxml(); //<-- this retrieves RUBBISH
At this point my string is mangled (just a few chinese characters at the start then rubbish) . Looks like an encoding issue.
I also tried the following:
_bstr_t superMarkup = _bstr_t(markup); //did my stuff superMarkup = pXMLDoc->Getxml(); markup = superMarkup;
but still I am getting the same result.
Even if I call GetXML() without changing anything in the xml document I still get rubbish.
At this point if I try to assign the mangled pointer to another pointer it will trow an error:
Attempted to restore write protected memory. this is often an indication that other memory is corrupted.
Any suggestion?
EDIT1:
I found out this is happening in relation to the size of the XML string. If it happens on a given XML string and I reduce the size (keeping the same schema) it will work fine. Looks like MSXML2::DOMDocument40 has a limitation on size? In detail it happens if I have more than 16407 characters. I have one more GetXML will retrieve RUBBISH – if it’s <= 16407 everything works fine.
EDIT2:
Roddy was right – I was missing that _bstr_t is a class …
Rings any bell?
Cheers
Try replacing
with
BSTR is pretty much a dumb pointer, and I think that the return result of GetXML() is being converted to a temporary which is then destroyed by the time you get to see it. bstr_t wraps that with some smart-pointer goodness…
Note: Your ‘SuperMarkup’ thing did NOT do what I suggested. Again, BSTR is just a pointer, and doesn’t ‘own’ what it points to. bstr_t, on the other hand does. I think your GetXML() function is returning a bstr_t, which is then being deleted as it goes out of scope, leaving your BSTR pointing to memory that is no longer valid.