Menu

#10 Segmentation fault when using class method containing vector push_back inside OpenThreads::Thread run method.

v1.0_(example)
open
nobody
None
5
2017-03-08
2017-03-08
No

I am encountering segmentation fault in the following piece of code using OpenThreads::Thread.
I am running it in a linux environment which is SUSE Linux Enterprise Server 11 (x86_64), with the kernel version being Linux sfs-imcbuild-01 3.0.101-0.47.52-default.

class CMirroringHelper
{
public:

CMirroringHelper()
{
std::cout<<"Mirror Helper Constructor called"<<std::endl;
vecMir.reserve(100);
std::cout<<"Mirror Helper Vector capacity"<<vecMir.capacity()<<std::endl;
}
void MirroringHelperInit()
{
std::string it = "adf";
m_i = 4567;
vecMir.push_back(123); //Cause of Segmentation Fault
std::cout<<"The point of danger "<<m_i<<std::endl;
}
int m_i;

private:

std::vector <int> vecMir;
};

class CPurgerThread : public OpenThreads::Thread
{
public:
CPurgerThread();

virtual ~CPurgerThread();

virtual void run();

void stopThread();

private:
std::multiset<CTempLogElem> setTempLogElems;
std::vector< CLogPurger* > m_vLogPurger;
CMirroringHelper m_pMirroringHelper;
};

The issue happens when m_pMirroringHelper.MirroringHelperInit is called inside the run method of class CPurgerThread. When I am using vecMir[some_index] = 123, the program do not run into segmentation fault, only the push_back is causing the issue which I believe may be due to internal allocation in vector push_back method, which may have caused some kind of scope issue. Also when "m_pMirroringHelper" was declared as a pointer and then allocated using new in CPurgerThread, the program did not run into segmentation fault. I also tried declaring vecMir as a vector pointer inside CMirroringHelper, but it still ran into segmentation fault.
This issue was not seen in Visual Stuudio 2010 vc10 on Windows 7.

Thanks
MithunMohan

Discussion


Log in to post a comment.