Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 21994

Exceptions are not caught in a secondary thread, causing segfault

$
0
0

I cross-compile for a MIPS-based platform. The following code causes segfault, backtrace leads to __cxa_throw:

#include <cstdio>
#include <exception>
#include <thread>

#include <execinfo.h>
#include <signal.h>
#include <unistd.h>

void handler(int sig) {
  fprintf(stderr, "Error: signal %d:\n", sig);
  void *array[10];
  size_t size;

  // get void*'s for all entries on the stack
  size = backtrace(array, 10);

  fprintf(stderr, "Error: signal %d:\n", sig);
  // print out all the frames to stderr
  backtrace_symbols_fd(array, size, STDERR_FILENO);
  exit(1);
}

void deco()
{
    puts("Secondary thread started");
    for (int i = 0; i < 100; ++i)
    {
        try {
            puts("throwing");
            throw std::runtime_error("test exception");
        }
        catch (std::exception & e)
        {
            puts("catched");
            puts(e.what());
        }
        puts("going on");
    }
    puts("Secondary thread ending");
}


int main()
{
    signal(SIGSEGV, handler);
    std::thread t2(deco);
    t2.join();
    return 0;
}

my makefile:

all: build

build: 
    /opt/mips/bin/mips-linux-gnu-g++  \
          -std=c++11 -O3 -mips32r2 -mtune=xburst -mmxu2 -mhard-float -mel -static -pthread -g -rdynamic \
          -Wl,--no-export-dynamic -Wl,--exclude-libs,ALL -Wl,--gc-sections -Wl,--as-needed \
          cv_test.cpp -o j_test \
          -lm -latomic -lrt
    cp ./j_test /srv/tftp/j_test

clean:
    rm -rf *.o j_test

The compiler is some ingenic custom build: mips-linux-gnu-g++ (Ingenic r3.2.1-gcc520 2017.12-15) 5.2.0

No crash happens if deco() is run in the main thread instead of a secondary thread. Any clues? Probably a well-known gcc standard library issue? Are there a workaround?


Viewing all articles
Browse latest Browse all 21994

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>