-1

first post ever here... I'm trying to get this code to work, it compiles but throws out a seg. fault when I try to access the data using the pointer after the function returns. I guess the data is deleted after the function ends so the pointer is lost. I have searched a lot and thought that the only way to fix it is if some else can take a look at my code and point me in the right direction. Thanx a lot in advance.

/* 
 * Compile with:
 * g++ -std=c++11 -o gsoap_pf_download_config main.cpp soapC.cpp \
 * soapBasicHttpBinding_USCOREIPagaFacilCobroServiceProxy.cpp -lgsoap++ 
 */

#include "soapBasicHttpBinding_USCOREIPagaFacilCobroServiceProxy.h"
#include "BasicHttpBinding_USCOREIPagaFacilCobroService.nsmap"
#include <string>

std::string BVserialNum = "ACF90004";
std::string BVid =        "215";
std::string latitud =     "0";
std::string longitud =    "0";

class SoapClient {
  ns2__ParametrosEnvio *parameters;
  ns2__ArrayOfParametrosEnvio *parametersArray;

  public:
    int downloadConfig();

  private:
    std::string getToken(std::string date, std::string time);
    std::string getProcessKey(std::string token);
    int createClient(_tempuri__AperturaTerminalConsumo &client);
};

int SoapClient::createClient(_tempuri__AperturaTerminalConsumo &client) {
  timeval curTime;
  gettimeofday(&curTime, NULL);
  std::string date = getDate(curTime);
  std::string time = getTime(curTime);
  std::string timeMicro = getTimeWithMicroSecs(curTime);

  std::string token = getToken(date, time);
  std::string processKey = getProcessKey(token);

  ns2__ParametrosEnvio *arrayParam1 = new ns2__ParametrosEnvio(); 
  ns2__ParametrosEnvio *arrayParam2 = new ns2__ParametrosEnvio();
  ns2__ParametrosEnvio *arrayParam3 = new ns2__ParametrosEnvio();
  ns2__ParametrosEnvio *arrayParam4 = new ns2__ParametrosEnvio();
  ns2__ParametrosEnvio *arrayParam5 = new ns2__ParametrosEnvio();
  ns2__ParametrosEnvio *arrayParam6 = new ns2__ParametrosEnvio();
  ns2__ParametrosEnvio *arrayParam7 = new ns2__ParametrosEnvio();

  std::string tokenStr = "TerminalToken";
  std::string processKeyStr = "LlaveProceso";
  std::string serialNumStr = "TerminalSerie";
  std::string dateStr = "TerminalFecha";
  std::string timeStr = "TerminalHora";
  std::string latStr = "Latitud";
  std::string lonStr = "Longitud";

  arrayParam1->Key = &tokenStr;
  arrayParam1->Data = &token;
  arrayParam2->Key = &processKeyStr;
  arrayParam2->Data = &processKey;
  arrayParam3->Key = &serialNumStr;
  arrayParam3->Data = &BVserialNum;
  arrayParam4->Key = &dateStr;
  arrayParam4->Data = &date;
  arrayParam5->Key = &timeStr;
  arrayParam5->Data = &time;
  arrayParam6->Key = &latStr;
  arrayParam6->Data = &latitud;
  arrayParam7->Key = &lonStr;
  arrayParam7->Data = &longitud;

  std::vector<ns2__ParametrosEnvio*  > parametersVector;
  parametersVector.push_back(arrayParam1);
  parametersVector.push_back(arrayParam2);
  parametersVector.push_back(arrayParam3);
  parametersVector.push_back(arrayParam4);
  parametersVector.push_back(arrayParam5);
  parametersVector.push_back(arrayParam6);
  parametersVector.push_back(arrayParam7);

  ns2__ArrayOfParametrosEnvio parametersArray;
  parametersArray.ParametrosEnvio = parametersVector;

  ns2__ParametrosEnvio parameters;
  parameters.ListaDatos = &parametersArray;

  client.Parametros = &parameters;

  int i = 0;
  for (std::vector<ns2__ParametrosEnvio*  >::iterator it = \
    parametersVector.begin(); it != parametersVector.end(); ++it) {
    std::cout << *it << ":" \
    << *client.Parametros->ListaDatos->ParametrosEnvio.at(i)->Key << ":" \
    << *client.Parametros->ListaDatos->ParametrosEnvio.at(i)->Data \
    << std::endl;
    ++i;
  }

  return SOAP_OK;
}

int SoapClient::downloadConfig() {
  BasicHttpBinding_USCOREIPagaFacilCobroServiceProxy service;
  _tempuri__AperturaTerminalConsumo client;
  _tempuri__AperturaTerminalConsumoResponse response;
  createClient(client);

  std::cout << *client.Parametros->ListaDatos->ParametrosEnvio.at(0)->Key << std::endl;
  std::cout << *client.Parametros->ListaDatos->ParametrosEnvio.at(1)->Key << std::endl;
  std::cout << *client.Parametros->ListaDatos->ParametrosEnvio.at(2)->Key << std::endl;
  std::cout << *client.Parametros->ListaDatos->ParametrosEnvio.at(3)->Key << std::endl;
  std::cout << *client.Parametros->ListaDatos->ParametrosEnvio.at(4)->Key << std::endl;
  std::cout << *client.Parametros->ListaDatos->ParametrosEnvio.at(5)->Key << std::endl;
  std::cout << *client.Parametros->ListaDatos->ParametrosEnvio.at(6)->Key << std::endl;

  int ret = service.AperturaTerminalConsumo(&client, response);
  if (ret == SOAP_OK) {
    std::cout << "id:" << *response.AperturaTerminalConsumoResult-> \
    TerminalConsumo->Id << std::endl;
    std::cout << "locality:" << *response.AperturaTerminalConsumoResult-> \
    TerminalConsumo->LocalidadDispositivoId << std::endl;
    std::cout << "route:" << *response.AperturaTerminalConsumoResult-> \
    Ruta->Id << std::endl;
    std::cout << "fare:" << *response.AperturaTerminalConsumoResult-> \
    Ruta->Tarifa << std::endl;
  }
  else
    service.soap_stream_fault(std::cerr);

  service.destroy(); // delete data and release memory
}

int main()
{ 
  SoapClient client;
  client.downloadConfig();
}

``

1 Answers1

1

Lets look at these lines:

ns2__ArrayOfParametrosEnvio parametersArray;
...
ns2__ParametrosEnvio parameters;
parameters.ListaDatos = &parametersArray;
client.Parametros = &parameters;

Here you declare the local variables parametersArray and parameters, then you save pointers to these variables.

When the function returns these variables go out of scope and the objects are destructed. The pointers you have saved no longer points to valid objects. Trying to use them will lead to undefined behavior.

You have two solutions: Either you don't use pointers, but copy the objects instead (my recommendation). Or you need to allocate these object dynamically with new (and remember to free them with delete).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thank you so much @Joachim Pilerborg you helped me a lot. I ended up using pointers and the problem was that I wasn't allocating memory for the strings. This [post](http://stackoverflow.com/questions/3350385/how-to-return-an-object-in-c) also helped me a lot. – danielmontero.cr Apr 26 '16 at 19:07