Ok - when using std::string* one should use soap_instantiate_std__string
instead of soap_malloc
which I did not find in the documentation, then everything works fine!
Assign value to std::string pointer created with soap_malloc
Question
I am currently implementing a webservice using gsoap version 2.8 and running into segmentation faults.
Therefor I allocate memory using soap_malloc like this:
OSoap *myObject = (OSoap *)soap_new_OSoap(this);
myObject->myString = (std::string*)soap_malloc(this, sizeof(std::string));
The source code of OSoap is generated using a wsdl and looks like this:
class SOAP_CMAC OSoap {
...
public:
std::string *myString; // optional attribute
...
}
Now i have a string allocated but how do I write content to this string?
myObject->myString->insert(0, "123");
and
*(myObject->myString) += "abc";
lead to segmentation faults.
std::string *abc = new std::string("abc");
myObject->myString = abc;
works but produces a memory leak which i try to avoid.
Searching google or stackoverflow for how to copy a string in c++ did not give me a hint how to solve the problem using std::string pointers
Solution
OTHER TIPS
I had the same problem. I saw that your std::string* is an "optional" attribute. After i wrote :
<xsd:element minOccurs="1" maxOccurs="1" name="myString" type="xsd:string"/>
it changed to a std::string in header ! I don't know if you use a xsd element but thats a way which works good.
Complex types can be instantiating by using soap_new_XXX
functions, like soap_new_std__string(soap, 1)
in this case. This function calls soap_instantiate_std__string
internally. Memory will be automatically released by gsoap.
See gsoap documentation chapter 9.13.1 Memory Allocation and Management Policies.