The last couple of days I've been working on a strange segmentation fault. The following code works just fine on my PC running Linux. But using the ARM crosscompiler suppplied by the manufactor of our embedded device, it gives a segmentation fault in routine s(). The SIGSEGV occurs at the destruction of a std::string. This also happens if you pass it as a variable. However, with non STL classes (e.g. int) it all works fine.
The code is compiled using an ARM cross compiler
arm-linux-g++ -Wall -otest -lpthread
arm-linux-strip test
All suggestions are welcome!
using namespace std;
void *ExecuteThreadMethod(void *AThread);
class Thread
{
private:
pthread_t internalThread;
sem_t finishedSemaphore;
public:
bool Terminated;
Thread()
{
Terminated = false;
sem_init (&finishedSemaphore, 0, 0);
}
~Thread()
{
sem_destroy (&finishedSemaphore);
}
void RunSigSegv() // <----- with the try...catch it causes a SIGSEGV in routine s() below
{
try
{
while (!Terminated)
{
cout << "ExecuteLooped" << endl;
}
sem_post(&finishedSemaphore);
}
catch(...)
{
}
}
void RunOk() // <----- without the try...catch, it runs fine
{
while (!Terminated)
{
cout << "ExecuteLooped" << endl;
}
sem_post(&finishedSemaphore);
}
void Start()
{
pthread_attr_t _attr;
pthread_attr_init(&_attr);
pthread_attr_setdetachstate(&_attr, PTHREAD_CREATE_DETACHED);
pthread_create (&internalThread, &_attr, ExecuteThreadMethod, this);
}
void Terminate()
{
Terminated = true;
sem_wait(&finishedSemaphore);
}
};
void *ExecuteThreadMethod(void *AThread)
{
((Thread *)AThread)->RunSigSegv();
pthread_exit(NULL);
}
Thread _thread;
void s()
{
string Test;
_thread.Start();
} // <----- destruction of std::string Test causes a SIGSEGV, without try...catch in the thread this doesn't happen
void t()
{
_thread.Terminate();
}
int main(void)
{
s();
t();
}