Cheedoong / xml2json

A header-only C++ library converts XML to JSON
MIT License
300 stars 102 forks source link

Code analysis warns about stacksize #5

Closed zoenie123 closed 8 years ago

zoenie123 commented 9 years ago

Maybe it's a good idea to look at the warning below of an analysis of Microsoft Visual Studio 2013.

c:\development\gatewaylight\xml2json.hpp(162): warning C6262: Function uses '65760' bytes of stack: exceeds /analyze:stacksize '16384'. Consider moving some data to heap.

std::string xml2json(const char *xml_str)
{
  //file<> fdoc("track_orig.xml");
  rapidxml::xml_document<> doc;
  doc.parse<0>(const_cast<char *>(xml_str));

  rapidjson::Document document;
  document.SetObject();
  rapidjson::Document::AllocatorType& allocator = document.GetAllocator();

  rapidxml::xml_node<> *xmlnode_chd;

  for(xmlnode_chd = doc.first_node(); xmlnode_chd; xmlnode_chd = xmlnode_chd->next_sibling()) {
    //cout << xmlnode_chd->name() << endl;
    rapidjson::Value jsvalue_chd;
    jsvalue_chd.SetObject();
    traverse_node(xmlnode_chd, jsvalue_chd, allocator);
    document.AddMember(rapidjson::StringRef(xmlnode_chd->name()), jsvalue_chd, allocator);
  }
  rapidjson::StringBuffer buffer;
  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  document.Accept(writer);
  return buffer.GetString();
}
CarlosRodriguezRamirez commented 9 years ago

try creating a pointer to xml_document, the problem will be fixed.

Cheedoong commented 9 years ago

@CarlosRodriguezRamirez Great! Thanks for your suggestion. Now it's fixed:

rapidxml::xml_document<> *xml_doc = new rapidxml::xml_document<>();
...
delete xml_doc;

@zoenie123 Please check out the latest version. Thanks also!