getting segmentation fault error in c++

getting segmentation fault in C++ code
I am new to c++, concising the code,my c++ code is giving me segmentation fault error, even debug is not detecting the exact error, please help me in this.

FetchingAndSortingFiles.h

FetchingAndSortingFiles *fileParamToSort;
std::vector<ABL_String> l_vecOfFileSortingOrderValues;
ABL_String m_lobName; //ABL_String is defined in framework ,it's just std::string
ABL_String m_filename;
unsigne int m_postalCode;
ABL_String filename;
std::vector<ABL_String> l_unsortedFileList;


FetchingAndSortingFiles.cpp

//mb_process() I am calling from main function from another file.
bool FetchingAndSortingFiles::mb_process()
{
// in below function I am fetching 3 files GSM_103.ps, GSM_101.ps ,GSM_104.ps and storing in l_unsortedFileList vector

m_ABL_DirectoryReader.mb_listDir(l_unsortedFileList,concatPathString.c_str(),m_searchOptn,false);

std::cout<<"l_unsortedFileListSize======="<<l_unsortedFileList.size()<<std::endl; //giving size 3

// allocating the memory equivalent to the size of vector,i think here i am committing the some mistake

fileParamToSort=new FetchingAndSortingFiles[l_unsortedFileList.size()];

for (int m_ifileIndex = 0; m_ifileIndex < l_unsortedFileList.size(); m_ifileIndex++)
{
filename =(l_unsortedFileList[m_ifileIndex]);
std::cout<<" filename====="<< filename<<std::endl; //giving first file which is GSM_103.ps
//in below function mb_extractDelimitedValues I am taking file one by one and segregating them based on delimiter '_'
//for example- if i take first file GSM_103 then segregating them through delimeter '_' and then storing GSM ,103 in vector l_vecOfFileSortingOrderValues like below

l_vecOfFileSortingOrderValues = FetchingAndSortingFiles::mb_extractDelimitedValues(filename,'_');

//in below code i am retreiving the values GSM 103 and so on from vector and storing them in fileParamToSort pointer ,m_direcDate,numOfPages values i am getting from another functions
if (l_vecOfFileSortingOrderValues.size() ==2)
{
fileParamToSort[m_ifileIndex].m_lobName = l_vecOfFileSortingOrderValues[0].mb_getSTLString();
fileParamToSort[m_ifileIndex].m_postalCode = atoi((l_vecOfFileSortingOrderValues[5].mb_getSTLString()).c_str());
fileParamToSort[m_ifileIndex].m_filename=filename;
}

}//for loop ends
}

in destructor in same file

FetchingAndSortingFiles::~FetchingAndSortingFiles()
{
std::cout<<"fileParamToSort======="<<fileParamToSort<<std::endl;
if(fileParamToSort){
delete [] fileParamToSort;
}
fileParamToSort=NULL;
}

Output:

fileParamToSort=======0x196afe8
Segmentation fault (core dumped)

Last edited on
What looks a bit suspicious is that in bool FetchingAndSortingFiles::mb_process() you have a local var fileParamToSort but there is also a class var with the same name in your destructor.
Your program probably crashes at this point in your destructor delete [] fileParamToSort;
Check that the class var is properly is properly created.
This could be a problem:
1
2
3
4
5
6
else
{
*m_ABL_ServicePtr<< DEBUG <<"file "<<filename<<"is not in correct format"<<Endl;
delete [] fileParamToSort; // Deleting fileParamToSort without setting it to nullptr
return false;
}


In the destructor of FetchingAndSortingFiles you may delete fileParamToSort twice. Why don't you use a std::vector like you did i.e. with l_unsortedFileList?

In FetchingAndSortingFiles.h: Are these variables global?
hi Thomas1965

fileParamToSort is not local ,it's already declared in FetchingAndSortingFiles.h as

FetchingAndSortingFiles *fileParamToSort;

i am allocating the memory to fileParamToSort like below:-

fileParamToSort=new FetchingAndSortingFiles[l_unsortedFileList.size()];




hi coder777 ,
else portion is not a problem since compiler is executing only 'if' part not 'else' part, but yes i will correct it .
Topic archived. No new replies allowed.