Вопрос

Using Xerces C++ I generated the typical C++ code from the below schema. Upon serialization of an object I get an access violation. I stepped through the code way down the stack until some templated insertion code for a std::basic_string and it seems to be happening there.

I could go into where in the generated code the problem happens. But it seems to be overkill. I'm sure this is an issue with my code.

My Code is below.

#include <sstream>
#include <iostream>
#include "..\XMLObjects\SomeXML.hxx"

void serializeObject(Object* mess, std::string& result);
void create();

int _tmain(int argc, _TCHAR* argv[])
{
create();
return 0;
}

void create()
{
std::string result;

objectType oType("Status");
std::auto_ptr<Object> obj( &Object(oType) );

time_t seconds=time(NULL);

Object::status_type s(seconds);
obj->status().set(s);
obj->status()->timeOfUpdate();
serializeObject(obj.get(), result);

}

void serializeObject(Object* mess, std::string& result)
{

std::ostringstream buff;
xml_schema::namespace_infomap nsm;
nsm[""].name = "";
nsm[""].schema = "SomeXML.xsd";

try
{
    Object_(buff, *mess, nsm, "UTF-8", xml_schema::flags::no_xml_declaration);
}
catch (const xml_schema::exception& e)
{
    std::cout << e << std::endl;
    return;
}
catch(std::exception& ex)
{
    std::string info(" Caught the exception ");
    info+=ex.what();

}
catch(...)
{
    std::string info(" Caught an exception ");

}

    result=buff.str().c_str();

}

The following is the schema I'm using to generate the code.

    <?xml version="1.0" encoding="utf-8"?>

  <!--<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.com/SomeXML">-->

  <xsd:complexType name ="Status" >
    <xsd:sequence>
      <xsd:element name="timeOfUpdate" type="xsd:unsignedLong" minOccurs="1" maxOccurs="1"/>
    </xsd:sequence>
  </xsd:complexType>


  <xsd:simpleType name="objectType">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="Status"/>
      <xsd:enumeration value="Thing A"/>
      <xsd:enumeration value="Thing B"/>
    </xsd:restriction>
  </xsd:simpleType>

  <xsd:complexType name="Object" >
   <xsd:sequence>
     <xsd:element name="objectType" type ="objectType" minOccurs="1" maxOccurs="1" />
      <xsd:element name ="status" type ="Status" minOccurs="0" maxOccurs="1"/>
    </xsd:sequence>

  </xsd:complexType >


  <xsd:element name="Object" type="Object" />
</xsd:schema>
Это было полезно?

Решение

std::auto_ptr<Object> obj( &Object(oType) );

This is most likely one source of headache. This looks like you're creating a temporary, and then taking it's address, and storing it in an auto_ptr.

The temporary then immediately goes out of scope, and you're left with a dangling pointer. Also, when it reaches the end of the scope, it'll try to delete a pointer that was originally on the stack.

Try replacing it with

std::auto_ptr<Object> obj( new Object(oType) );

or, if you're using a C++11 compatible compiler, use

std::unique_ptr<Object> obj( new Object(oType) );

since auto_ptr has been deprecated in the latest standard.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top