Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xml_node is distroyed after loading #71

Closed
changshen opened this issue Dec 4, 2015 · 3 comments
Closed

xml_node is distroyed after loading #71

changshen opened this issue Dec 4, 2015 · 3 comments
Labels

Comments

@changshen
Copy link

class myxmltool
{
public:
    string filename;
    xml_node root;
    myxmltool(string fname)
    {
        xml_document doc;
        doc.load_file(fname.c_str());
        root = xml_node(doc.root());  //root is created and has contents
    }

};

void main()
{
    myxmltool tool = myxmltool("c:\\myxmlfile.xml");
    xml_node rootnode = tool.root;  //root was not there.  it was distroyed already

}
@jima80525
Copy link

Isn't that because the doc has already been destructed? If you make the
doc a member of the class so it has not been freed, it will work.
Change the class to:

class myxmltool
{
public:
string filename;
xml_node root;
xml_document doc;

    myxmltool(string fname)
    {
        doc.load_file(fname.c_str());
        root = xml_node(doc.root()); //root is created and has contents
    }

};

jima

On Fri, Dec 4, 2015 at 8:39 AM, changshen notifications@github.com wrote:

class myxmltool
{
public:
string filename;
xml_node root;
myxmltool(string fname)
{
xml_document doc;
doc.load_file(fname.c_str());
root = xml_node(doc.root()); //root is created and has contents
}

};

void main()
{
myxmltool tool = myxmltool("c:\myxmlfile.xml");
xml_node rootnode = tool.root; //root was not there. it was distroyed
already

}


Reply to this email directly or view it on GitHub
#71.

@zeux
Copy link
Owner

zeux commented Dec 5, 2015

xml_document object controls the lifetime of the tree; xml_node objects are just handles to nodes of that tree. Destroying the xml_document object leads to the entire tree being destroyed; it's invalid to use the nodes from the tree after that. This is described in more detail in this section of the manual: http://pugixml.org/docs/manual.html#dom.cpp.

@zeux zeux closed this as completed Dec 5, 2015
@zeux zeux added the invalid label Dec 5, 2015
@changshen
Copy link
Author

Thanks zeux and jima
I thought of this and tried to have xml_document as a member of extended class. But compiler complained :
Error 2 error C2248: 'pugi::xml_document::xml_document' : cannot access private member declared in class 'pugi::xml_document'
even with empty constructor
myxmltool(string fname){}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants