How to fix RapidXML String ownership concerns?
- by Roddy
RapidXML is a fast, lightweight C++ XML DOM Parser, but it has some quirks.
The worst of these to my mind is this:
3.2 Ownership Of Strings.
Nodes and attributes produced by RapidXml do not
own their name and value strings. They
merely hold the pointers to them. This
means you have to be careful when
setting these values manually, by
using xml_base::name(const Ch *) or
xml_base::value(const Ch *) functions.
Care must be taken to ensure that
lifetime of the string passed is at
least as long as lifetime of the
node/attribute. The easiest way to
achieve it is to allocate the string
from memory_pool owned by the
document. Use
memory_pool::allocate_string()
function for this purpose.
Now, I understand it's done this way for speed, but this feels like an car crash waiting to happen. The following code looks innocuous but 'name' and 'value' are out of scope when foo returns, so the doc is undefined.
void foo()
{
char name[]="Name";
char value[]="Value";
doc.append_node(doc.allocate_node(node_element, name, value));
}
The suggestion of using allocate_string() as per manual works, but it's so easy to forget.
Has anyone 'enhanced' RapidXML to avoid this issue?