From: <m....@sy...> - 2005-06-07 16:52:20
|
Hi, I have am trying to use your API with MSVC71. Most things seem to work okay, but when I have a line of code like the following: const xmlpp::Node::NodeList& m_nodeList = root->get_children(); xmlpp::Node::NodeList::const_iterator itr = m_nodeList.begin(); After running my program through a debugger, I found that it's when m_nodeList.begin() is called that the problem arises. I can make calls to m_nodeList.size() and empty() without a problem, it's just the begin one. The error I get is "The application has requested the runtime to terminate in an unusual way..." Does anyone know what I am doing wrong? Thanks in advance for any help, Matt Smart <m....@sy...> |
From: Darko M. <da...@uv...> - 2005-06-07 19:14:54
|
m....@sy... wrote: > Hi, I have am trying to use your API with MSVC71. Most things seem to work okay, but when I have a line of code like the following: > > const xmlpp::Node::NodeList& m_nodeList = root->get_children(); > xmlpp::Node::NodeList::const_iterator itr = m_nodeList.begin(); > > After running my program through a debugger, I found that it's when m_nodeList.begin() is called that the problem arises. I can make calls to m_nodeList.size() and empty() without a problem, it's just the begin one. The error I get is "The application has requested the runtime to terminate in an unusual way..." > > Does anyone know what I am doing wrong? Thanks in advance for any help, You do it wrong. get_children return a list container that should contain all children elements. However if there is no children of that node get_children returns empty list. You must check if list contains something. You can do it like this: xmlpp::Node::NodeList nl = node->get_children(); if ( !nl.empty() ) { xmlpp::Node::NodeList::iterator it = nl.begin(); //other code here } Darko |
From: Jonathan W. <co...@co...> - 2005-06-09 16:32:22
|
On Tue, Jun 07, 2005 at 04:14:41PM -0300, Darko Miletic wrote: > m....@sy... wrote: > >Hi, I have am trying to use your API with MSVC71. Most things seem to work > >okay, but when I have a line of code like the following: > > > > const xmlpp::Node::NodeList& m_nodeList = root->get_children(); > > xmlpp::Node::NodeList::const_iterator itr = m_nodeList.begin(); > > > >After running my program through a debugger, I found that it's when > >m_nodeList.begin() is called that the problem arises. I can make calls to > >m_nodeList.size() and empty() without a problem, it's just the begin one. > >The error I get is "The application has requested the runtime to terminate > >in an unusual way..." > > > >Does anyone know what I am doing wrong? Thanks in advance for any help, > > You do it wrong. get_children return a list container that should > contain all children elements. However if there is no children of that > node get_children returns empty list. You must check if list contains > something. You can do it like this: > > xmlpp::Node::NodeList nl = node->get_children(); > if ( !nl.empty() ) { > xmlpp::Node::NodeList::iterator it = nl.begin(); > //other code here > } Surely not?!?! If the container is empty calling begin() will return an iterator that compares equal to end(). _dereferencing_ that iterator would be an error, but not simply copying it. jon -- "What I tell you three times is true" - The Hunting of the Snark |
From: Darko M. <da...@uv...> - 2005-06-09 16:54:02
|
Jonathan Wakely wrote: > Surely not?!?! > > If the container is empty calling begin() will return an iterator that > compares equal to end(). _dereferencing_ that iterator would be an > error, but not simply copying it. It will be error if you use (as you did) reference to NodeList. You wrote the code like this: const xmlpp::Node::NodeList& m_nodeList = root->get_children(); that means that you take reference to the temporary created nodelist object. It is invalid after that line is executed an hence the access violation. Therefore you need to copy it. Instead of const xmlpp::Node::NodeList& use just xmlpp::Node::NodeList that way the list will be copied as it should because the declaration of xmlpp::Node::get_children is like this: NodeList get_children(const std::string& name = std::string()); So no reference is returned here. The code should be like this: xmlpp::Node::NodeList m_nodeList = root->get_children(); xmlpp::Node::NodeList::const_iterator itr = m_nodeList.begin(); Darko |
From: Jonathan W. <co...@co...> - 2005-06-09 17:29:34
|
On Thu, Jun 09, 2005 at 01:53:03PM -0300, Darko Miletic wrote: > Jonathan Wakely wrote: > > >Surely not?!?! > > > >If the container is empty calling begin() will return an iterator that > >compares equal to end(). _dereferencing_ that iterator would be an > >error, but not simply copying it. > > It will be error if you use (as you did) reference to NodeList. You > wrote the code like this: > > const xmlpp::Node::NodeList& m_nodeList = root->get_children(); > > that means that you take reference to the temporary created nodelist > object. It is invalid after that line is executed an hence the access Not true, the standard requires that a temporary object bound to a const reference is kept in scope as long as the reference. > violation. Therefore you need to copy it. Instead of const > xmlpp::Node::NodeList& use just xmlpp::Node::NodeList that way the list > will be copied as it should because the declaration of > xmlpp::Node::get_children is like this: > > NodeList get_children(const std::string& name = std::string()); > > So no reference is returned here. > > The code should be like this: > > xmlpp::Node::NodeList m_nodeList = root->get_children(); > xmlpp::Node::NodeList::const_iterator itr = m_nodeList.begin(); Not true, the original code should work AFAICT. jon -- "An alcoholic is someone you don't like who drinks as much as you do." - Dylan Thomas |