1

I'm using xerces-c++ I don't seem to really get the use of XMLString::release ( XMLCh ** buf) so right now I'm using it every time I want to reuse a local variable like this:

XMLCh* xmlStringVar = XMLString::Transcode("name");
XMLCh* fieldName = fieldsNodeList[NbreFlds]->getAttribute(xmlStringVar));
XMLString::release(&xmlStringVar);
xmlStringVar = XMLString::Transcode("id");
XMLCh* fieldId = fieldsNodeList[NbreFlds]->getAttribute(xmlStringVar));

please correct to me this code if something is wrong and if you got a clearer description of the function XMLString::release please inform me.

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
Joy
  • 1,707
  • 7
  • 29
  • 44

2 Answers2

1

Looks perfectly valid. As the documentation for XMLString::release says use it to free memory allocated by the transcode() function.

Clemens
  • 1,744
  • 11
  • 20
1

Xerces documentation says: "The implementation will call MemoryManager::deallocate and then turn the string to a null pointer." To assign the string to a null pointer it requires address of the string pointer. If it was deallocation only, passing pointer (instead of address of the pointer) would be sufficient.

xaero99
  • 327
  • 1
  • 5
  • how is that even possible !? once MemoryManager::deallocate is launched , the variable doesn't exist anymore it's deleted for good , so how does it turn the variable again to a null pointer after it deleted it ! – Joy Mar 26 '12 at 14:22
  • Actually the **memory space pointed by the pointer** is deleted, not the pointer itself. After this deletion, the pointer becomes a dangling pointer which means it points to an invalid memory location. In order to prevent accessing an invalid memory, it is a good practice to assign the dangling pointer to null. – xaero99 Mar 26 '12 at 18:34
  • ok, it seems reasonable now! thanks a lot that was really helpful. – Joy Mar 27 '12 at 10:08