You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by we...@ilt.fhg.de on 2000/11/28 12:30:54 UTC

Access Violation error

Hi,

I want to change the values of some attributes in a DOM-Document. For that I have to find
specified DOM_Element - nodes and than call setAttribute(key, value) on that elements to 
change the attribute. The following shows the listing of a small sample program (I'm using
VC++6):

#include <string>
#include <iostream>

#include <iltxerces.h> //some utilities and a simple wrapper on top of Xerces

using namespace std;

DOM_Element * getElement(DOM_Element docRoot)
{
	DOM_NodeList list = docRoot.getElementsByTagName("child");
	DOM_Node node = list.item(0);
	DOM_Element * el = (DOM_Element*)&node;

	el->setAttribute("attribute", "2");
	return el;
}

void main()
{
	//the xml-document
	string xmlDoc = "<?xml version='1.0' encoding='us-ascii'?>";
	       xmlDoc = xmlDoc + "<root> <child attribute=\"1\"> </child> </root>";

	//defined in iltxerces.h
	ILTParser iltParser;
	DOM_Element docRoot = iltParser.parse("", xmlDoc);

	DOM_Element * el = getElement(docRoot);

	DOM_NodeList list = docRoot.getElementsByTagName("child");
	DOM_Node node = list.item(0);
	DOM_Element * el2 = (DOM_Element*)&node;
	string str = U::DOMString2string(el2->getAttribute("attribute"));
	//str no has the value "2"

	el->setAttribute("attribute", "3");
	//at this point an "Access Violation" error is thrown
}

The function getElement(...) obviously changes the entry in my DOM-tree but DOM_Element * el 
seems not to point to a valid element within the DOM-tree after returning from getElement(..).  
Could anybody explain me what is the reason for that?

Tanks in advance 

Rolf

-------------------------------------
Rolf Wester
wester@ilt.fhg.de

Re: Access Violation error

Posted by we...@ilt.fhg.de.
> > DOM_Element * getElement(DOM_Element docRoot)
> > {
> > 	DOM_NodeList list = docRoot.getElementsByTagName("child");
> > 	DOM_Node node = list.item(0);
> > 	DOM_Element * el = (DOM_Element*)&node;
> > 
> > 	el->setAttribute("attribute", "2");
> > 	return el;
> > }
> >
> > [ ... ]
> >
> > The function getElement(...) obviously changes the entry in my DOM-tree but DOM_Element * el 
> > seems not to point to a valid element within the DOM-tree after returning from getElement(..).
> 
> You are passing a pointer to an object on the stack. That object is
> destroyed when the getElement() function returns, thus the pointer is left
> pointing at a no-longer-initialized chunk of memory.

How is the attribute of the element in the xml-document changed? Doesn't el
point to this element? 
 	el->setAttribute("attribute", "2");

> 
> Since DOM_Node and children are light weight, the better choice is to pass
> the entire object rather than a pointer:
> 
Could you please tell me what "light weight" means?

> DOM_Element getElement(DOM_Element docRoot)
> {
>   // ....
>   DOM_Element el = (DOM_Element &)node;
>   el.setAttribute("attribute", "2");
>   return el;
> }
> 
> 
> --Bill
> 

Thanks for your answer.

--Rolf

-------------------------------------
Rolf Wester
wester@ilt.fhg.de

Re: Access Violation error

Posted by Bill Schindler <de...@bitranch.com>.
> DOM_Element * getElement(DOM_Element docRoot)
> {
> 	DOM_NodeList list = docRoot.getElementsByTagName("child");
> 	DOM_Node node = list.item(0);
> 	DOM_Element * el = (DOM_Element*)&node;
> 
> 	el->setAttribute("attribute", "2");
> 	return el;
> }
>
> [ ... ]
>
> The function getElement(...) obviously changes the entry in my DOM-tree but DOM_Element * el 
> seems not to point to a valid element within the DOM-tree after returning from getElement(..).

You are passing a pointer to an object on the stack. That object is
destroyed when the getElement() function returns, thus the pointer is left
pointing at a no-longer-initialized chunk of memory.

Since DOM_Node and children are light weight, the better choice is to pass
the entire object rather than a pointer:

DOM_Element getElement(DOM_Element docRoot)
{
  // ....
  DOM_Element el = (DOM_Element &)node;
  el.setAttribute("attribute", "2");
  return el;
}


--Bill