1

Today, I posted a problem about a segmentation fault after destruction of a std::string (see this post). I've stripped the code so that I don't use the STL and still have a segmentation fault sometimes.

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 just before catch (...).

This problems seems to have a link with this post in Google Groups, but I haven't found any solution yet.

The code is compiled using an ARM cross compiler

Any suggestions are still welcome!


#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *ExecuteThreadMethod(void *AThread);

class Thread
{
  private:
    pthread_t internalThread;
  public:
    void RunSigSegv()
    {
      try
      {
        for (int i = 0; i < 200; i++)
        {
          usleep(10000);
        }
      } // <---- Segmentation fault occurs here
      catch(...)
      {
      }
    }

    void Start()
    {
      pthread_attr_t _attr;

      pthread_attr_init(&_attr);
      pthread_attr_setdetachstate(&_attr, PTHREAD_CREATE_DETACHED);
      pthread_create (&internalThread, &_attr, ExecuteThreadMethod, this);
    }
};

void *ExecuteThreadMethod(void *AThread)
{
  ((Thread *)AThread)->RunSigSegv();
  pthread_exit(NULL);
}

Thread _thread1;
Thread _thread2;
Thread _thread3;
Thread _thread4;

void s()
{
  _thread1.Start();
  _thread2.Start();
  _thread3.Start();
  _thread4.Start();
}

int main(void)
{
  s();
  usleep(5000000);
}

2 Answers 2

1

I once encountered a problem like this which was caused by linking with a version of libstdc++ with no threading support, meaning that all threads shared a common exception handling stack with disastrous consequences.

Make sure the cross-compiler and its libraries were configured with --enable-threads=posix.

2
  • This piece of code is compiled without <code>libstdc++</code>. It shouldn't use any of the STL routines (as far as I know).
    – Laurens
    Oct 18, 2010 at 15:37
  • @Laurens: It will be included implicitly when linking with g++; it's needed for various bits of language support, including exception handling, as well as the Standard Library. Oct 18, 2010 at 15:53
0

Just a diagnostic question: What happens if you don't detach the thread in Start()? You'd have to pthread_join() the threads back in main().

Also, have you considered Boost's threads? That might be more appropriate since you're using C++ rather than C.

1
  • Unfortunately, I tried it with joinable threads and it makes no difference.
    – Laurens
    Oct 18, 2010 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.