segmantation Fault error SEGV_MAPERR - Address not mapped to object

Program received signal SIGSEGV, Segmentation fault
si_code: 1 - SEGV_MAPERR - Address not mapped to object.
0x9fffffffbe7080d0:0 in free+0xb0 () from /usr/lib/hpux64/libc.so.1

Hi ,

I have developed a class to read config file (flat file with space as a field seperator ) on plattform HP-UX B.11.23 IA 64 .

I am passing pointers to strings to a function which reads the config file and traverse line by line and retrieves value on the basis of key passed as parameter . I am getting above error .when i call this function more than once. First time call works fine but second time it fails with above error.

Please see code of the function
:
int CConfigReader::GetConfigValue(char *paramApp,string *ResultServer,string * ResultPort,string *ResultLogDir)
{
ifstream Stream (szConfigPath);

if (Stream.is_open())
{
while(!Stream.eof())
{
string buffer=NULL;
getline(Stream, buffer);
if(buffer.length() !=0)
{
if (buffer.find('#') == string::npos)
{
//ingnore the line header
printf("\n the red is %s \n",buffer);
string *pstrSplit=NULL;
int iNumWords =split(buffer, pstrSplit);
if(iNumWords != -1)
{
if (pstrSplit[0].compare(paramApp)!=0)
{

continue;
}
else
{
*ResultServer=pstrSplit[1];
*ResultPort=pstrSplit[2];
*ResultLogDir=pstrSplit[3];
printf("\n Serve-- %s Port --%s Log Directory --%s \ n",*ResultServer,*ResultPort,*ResultLogDir);
delete[] pstrSplit;
break;
}
}
else
{
cout << "Error in reading the line ";
return 1;
}

}
else
{
cout << "header line ignore it ";

}
}
else
{
cout << "empty line ignore it ";
}
}//end of while
}
else
{
cout << "unable to open the stream ";
return 1;
}
Stream.close();
return 0;
}

function call :
string tempServer;
string tempPort;
string tempLogDir;
// Install the SAP RFC functions that can be called.
CConfigReader config =CConfigReader(szConfigFile);
if (! config.GetConfigValue("MMSYNCH",&tempServer,&tempPort,&tempLogDir))
{
printf("sucessfully executed function \n");
printf("result MMSYNCH server is--> %s Port is --> %s log dir is -->%s \n",tempServer,tempPort,tempLogDir);
nPort=atoi(tempPort.c_str());
strcpy(szServer, tempServer.c_str());
strcpy(szLogDir, tempLogDir.c_str());

}
else
{
ErrorExit( table[0].ithandle, "Error in reading config file", errno);
}

Please help me in resolviong this error .

Code tags for code please. They make it readable. [ code ] stuff [ /code ] without the extra spaces in the tags.

Posting a member function out of context is pretty pointless since a class is bound to contain member variables of which we have no knowledge, could you please post the complete code for this class, including header, in code tags?

Compiling your executable with debugging support, and running it in a debugger, may tell you what line this segfault is happening on. The gcc flag for this is -ggdb, and the GNU tool for debugging is gdb, I am not certain what HP-UX specific tools there are for this.

Thanks for reply .
I have changed parametrs to this function from string * to char *
getConfigValue is function in class CConfigReader

CConfigReader .c
 
#include "CConfigReader.h"
using namespace std;
CConfigReader::CConfigReader()
{}
CConfigReader::CConfigReader(char *paramConfigpath)
{
memset(szConfigPath, ' ', sizeof( szConfigPath) );
memcpy(szConfigPath,paramConfigpath,sizeof( szConfigPath));
}
int CConfigReader::GetConfigValue(char *paramApp,int paramKey,char *ResultValue)
{
ifstream Stream (szConfigPath);

if (Stream.is_open())
{
while(!Stream.eof()) 
{
string buffer=NULL;
getline(Stream, buffer);
if(buffer.length() !=0)
{
if (buffer.find('#') == string::npos)
{
//ingnore the line header
string *pstrSplit=NULL;
int iNumWords =split(buffer, pstrSplit);
if(iNumWords != -1)
{
if (pstrSplit[0].compare(paramApp)!=0)
{

continue;
}
else
{

strcpy(ResultValue,pstrSplit[paramKey].c_str()); 
break;
}
}
else
{
cout << "Error in reading the line "<<endl;
return 1;
}

}
else
{
cout << "header line ignore it "<<endl;

}
}
else
{
cout << "empty line ignore it "<<endl;
}
}//end of while 
}
else
{
cout << "unable to open the stream ";
return 1;
}
Stream.close();
return 0; 
}
int CConfigReader::split(string strInput, string *&pstrResult)
{
// Reserve some memory, WATCH OUT! USER NEEDS TO FREE THE MEMORY!
if(pstrResult != NULL)
return -1; // We need a clean buffer, we could shoose to delete the inside,
else
pstrResult = new string[1];
string strTemp;
int iInputLen = strInput.length(), iNumWords = 0;
if(iInputLen)
{
for(int i = 0; i < iInputLen; i++)
{
if((strInput == ' ') && (!strTemp.empty())) // Split it if there is a word.
{
if(iNumWords > 0)
{
string *pstrTempRes = new string[iNumWords];
CopyBuffer(pstrTempRes, pstrResult, iNumWords); // Make a deep copy of the buffer.
delete[] pstrResult;
pstrResult = new string[iNumWords+1];
CopyBuffer(pstrResult, pstrTempRes, iNumWords);
pstrResult[iNumWords] = strTemp;
delete[] pstrTempRes;
iNumWords++;
}
else
{
pstrResult[iNumWords] = strTemp;
iNumWords++;
}
strTemp.erase(); //- Erase the temp-string, and start over.
i++; // SKIP THE SPACE!
}
strTemp += strInput;
}
if(strTemp.length() > 0)
{
if(iNumWords > 0)
{
string *pstrTempRes = new string[iNumWords];
CopyBuffer(pstrTempRes, pstrResult, iNumWords); // Make a deep copy of the buffer.
delete[] pstrResult;
pstrResult = new string[iNumWords+1];
CopyBuffer(pstrResult, pstrTempRes, iNumWords);
pstrResult[iNumWords] = strTemp;
delete[] pstrTempRes;
iNumWords++;
}
else
{
pstrResult[iNumWords] = strTemp;
iNumWords++;
}
}
}
return iNumWords;
}
void CConfigReader::CopyBuffer(string *pstrDest, string *pstrSource, int iDynamicLen)
{
for(int i = 0; i < iDynamicLen; i++)
 
{
pstrDest = pstrSource;
}
}
 
CConfigReader.h
 
#include <iostream>
#include <istream>
#include <fstream>
#include <istream>
#include <string>
#include <stdio.h>
using namespace std;
class CConfigReader
{
public:
char szConfigPath[100];
CConfigReader();
CConfigReader(char *paramConfigpath );
int GetConfigValue(char *paramApp,int paramKey,char *ResultValue);
private:
int split(string strInput, string *&pstrResult);
void CopyBuffer(string *pstrDest, string *pstrSource, int iDynamicLen);
};
 

function call is as folllows

char szLogDir[ MAX_PATH ];
enum AppConfig {AppName=0,Server,Port,LogDir};
 
AppConfig enLogDir=LogDir;
CConfigReader config =CConfigReader(szConfigFile);
if (! config.GetConfigValue("MMSYNCH",enLogDir,szLogDir))
{
printf("sucessfully executed function \n");

}
else
{ 
printf("Error in reading the file \n" );
exit ;
}

Thanks

CConfigReader .c
 
#include "CConfigReader.h"
using namespace std;
CConfigReader::CConfigReader()
{}

CConfigReader::CConfigReader(char *paramConfigpath)
{
  memset(szConfigPath, ' ', sizeof( szConfigPath) );
  memcpy(szConfigPath,paramConfigpath,sizeof( szConfigPath));
}
int CConfigReader::GetConfigValue(char *paramApp,int paramKey,char *ResultValue)
{
  ifstream Stream (szConfigPath);

  if (Stream.is_open())
  {
    while(!Stream.eof()) 
    {
      string buffer=NULL;
      getline(Stream, buffer);
      if(buffer.length() !=0)
      {
        if (buffer.find('#') == string::npos)
        {
          //ingnore the line header
          string *pstrSplit=NULL;
          int iNumWords =split(buffer, pstrSplit);
          if(iNumWords != -1)
          {
            if (pstrSplit[0].compare(paramApp)!=0)
            {
              continue;
            }
            else
            {
              strcpy(ResultValue,pstrSplit[paramKey].c_str()); 
              break;
            }
          }
          else
          {
            cout << "Error in reading the line "<<endl;
            return 1;
          }
        }
        else
        {
          cout << "header line ignore it "<<endl;
        }
      }
      else
      {
        cout << "empty line ignore it "<<endl;
      }
    }//end of while 
  }
  else
  {
    cout << "unable to open the stream ";
    return 1;
  }

  Stream.close();
  return 0; 
}

int CConfigReader::split(string strInput, string *&pstrResult)
{
  // Reserve some memory, WATCH OUT! USER NEEDS TO FREE THE MEMORY!
  if(pstrResult != NULL)
    return -1; // We need a clean buffer, we could shoose to delete the inside,
  else
    pstrResult = new string[1];

  string strTemp;
  int iInputLen = strInput.length(), iNumWords = 0;

  if(iInputLen)
  {
    for(int i = 0; i < iInputLen; i++)
    {
      if((strInput == ' ') && (!strTemp.empty())) // Split it if there is a word.
      {
        if(iNumWords > 0)
        {
          string *pstrTempRes = new string[iNumWords];
          CopyBuffer(pstrTempRes, pstrResult, iNumWords); // Make a deep copy of the buffer.
          delete[] pstrResult;
          pstrResult = new string[iNumWords+1];
          CopyBuffer(pstrResult, pstrTempRes, iNumWords);
          pstrResult[iNumWords] = strTemp;
          delete[] pstrTempRes;
          iNumWords++;
        }
        else
        {
          pstrResult[iNumWords] = strTemp;
          iNumWords++;
        }

        strTemp.erase(); //- Erase the temp-string, and start over.
        i++; // SKIP THE SPACE!
      }

      strTemp += strInput;
    }

    if(strTemp.length() > 0)
    {
      if(iNumWords > 0)
      {
        string *pstrTempRes = new string[iNumWords];
        CopyBuffer(pstrTempRes, pstrResult, iNumWords); // Make a deep copy of the buffer.
        delete[] pstrResult;
        pstrResult = new string[iNumWords+1];
        CopyBuffer(pstrResult, pstrTempRes, iNumWords);
        pstrResult[iNumWords] = strTemp;
        delete[] pstrTempRes;
        iNumWords++;
      }
      else
      {
        pstrResult[iNumWords] = strTemp;
        iNumWords++;
      }
    }
  }
  return iNumWords;
}

void CConfigReader::CopyBuffer(string *pstrDest, string *pstrSource, int iDynamicLen)
{
  for(int i = 0; i < iDynamicLen; i++)
  {
    pstrDest = pstrSource;
  }
}
CConfigReader.h
 
#include <iostream>
#include <istream>
#include <fstream>
#include <istream>
#include <string>
#include <stdio.h>

using namespace std;

class CConfigReader
{
public:
  char szConfigPath[100];
  CConfigReader();
  CConfigReader(char *paramConfigpath );
  int GetConfigValue(char *paramApp,int paramKey,char *ResultValue);
private:
  int split(string strInput, string *&pstrResult);
  void CopyBuffer(string *pstrDest, string *pstrSource, int iDynamicLen);
};

function call is as folllows

char szLogDir[ MAX_PATH ];
enum AppConfig {AppName=0,Server,Port,LogDir};
 
AppConfig enLogDir=LogDir;
CConfigReader config =CConfigReader(szConfigFile);
if (! config.GetConfigValue("MMSYNCH",enLogDir,szLogDir))
{
  printf("sucessfully executed function \n");
}
else
{ 
  printf("Error in reading the file \n" );
  exit ;
}

I'll take a closer look at this in a bit, had to fix the indentation first.