Search code examples
c++stdgsoap

Assign value to std::string pointer created with soap_malloc


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

  • 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!