Changeset 64:82ab6c4099b8 in rrlib_xml


Ignore:
Timestamp:
28.02.2016 13:27:54 (4 years ago)
Author:
Jens Wettach <wettach@…>
Branch:
default
Phase:
public
Message:

more elegant memory deallocation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tDocument.cpp

    r63 r64  
    3434//---------------------------------------------------------------------- 
    3535#include <iostream> 
     36#include <memory> 
    3637 
    3738extern "C" 
     
    183184const tNode &tDocument::FindNode(const std::string &name) const 
    184185{ 
    185   xmlXPathContext *xpath_context = xmlXPathNewContext(this->document); // requires cleanup 
     186  auto cleanup_path_context = [](xmlXPathContext * ptr) 
     187  { 
     188    xmlXPathFreeContext(ptr); 
     189  }; // clean up the XPath context 
     190  std::unique_ptr<xmlXPathContext, decltype(cleanup_path_context)> xpath_context(xmlXPathNewContext(this->document), cleanup_path_context); // create and initialize an XPath context for this document with auto cleanup 
     191 
    186192  if (xpath_context == nullptr) 
    187193  { 
     
    189195  } 
    190196 
    191   xmlXPathObject *xpath_object = xmlXPathEvalExpression((xmlChar*)name.c_str(), xpath_context); // requires cleanup 
     197  auto cleanup_path_object = [](xmlXPathObject * ptr) 
     198  { 
     199    xmlXPathFreeObject(ptr); 
     200  }; // clean up the XPath object 
     201  std::unique_ptr<xmlXPathObject, decltype(cleanup_path_object)> xpath_object(xmlXPathEvalExpression((xmlChar*)name.c_str(), xpath_context.get()), cleanup_path_object); // create and initialize an XPath object as query result with auto cleanup 
     202 
    192203  if (xpath_object == nullptr) 
    193204  { 
    194     xmlXPathFreeContext(xpath_context); // clean up the XPath context 
    195205    throw tException("Could not evaluate the XPath expression!"); 
    196206  } 
     
    198208  if ((xpath_object->nodesetval == nullptr) || (xpath_object->nodesetval->nodeTab == nullptr)) 
    199209  { 
    200     xmlXPathFreeObject(xpath_object); // clean up the XPath object 
    201     xmlXPathFreeContext(xpath_context); // clean up the XPath context 
    202210    throw tException("Could not find the expected node!"); 
    203211  } 
    204212  xmlNode *node = xpath_object->nodesetval->nodeTab[0]; // fetch node from query result 
    205   xmlXPathFreeObject(xpath_object); // clean up the XPath object 
    206   xmlXPathFreeContext(xpath_context); // clean up the XPath context 
    207213  return *reinterpret_cast<tNode *>(node); 
    208214} 
Note: See TracChangeset for help on using the changeset viewer.