synchronous cancel and "terminate called without an active exception"

Pavel Emeliyanenko asm@mpi-sb.mpg.de
Fri Jun 1 12:45:00 GMT 2007


Hi all,

please don't panish me, I know there were plenty of discussions concerning 
this subject, however I'm a newbie in multi-threaded programming :-) and 
couldn't find a good explanation to my problem in former discussions..
so, perhaps somebody could "redirect" my question to the appropriate thread..

At the end you can see a piece of code. The problem is the following:
for some reason the 'new operator' affects thread cancellation: namely
if one uses the 'new operator' which is able to throw exceptions then thread
cancellation results in abort with 
  "terminate called without an active exception".. 

here is the gdb trace:

#0  0xb7c6783b in raise () from /lib/tls/libc.so.6
#1  0xb7c68fa2 in abort () from /lib/tls/libc.so.6
#2  0xb7f123ce in __gnu_cxx::__verbose_terminate_handler () 
at ../../.././libstdc++-v3/libsupc++/vterminate.cc:96
#3  0xb7f0fed5 in __cxxabiv1::__terminate (handler=0xb7f12260 
<__gnu_cxx::__verbose_terminate_handler()>)
    at ../../.././libstdc++-v3/libsupc++/eh_terminate.cc:43
#4  0xb7f0ff12 in std::terminate () 
at ../../.././libstdc++-v3/libsupc++/eh_terminate.cc:53
#5  0xb7f0fd74 in __gxx_personality_v0 (version=1, actions=10, 
exception_class=3083066116, ue_header=0x0, context=0xb7c3d968)
    at ../../.././libstdc++-v3/libsupc++/eh_personality.cc:420
#6  0xb7d78776 in _Unwind_ForcedUnwind_Phase2 (exc=0xb7c3ddd0, 
context=0xb7c3d968) at unwind.inc:180
#7  0xb7d78857 in _Unwind_ForcedUnwind (exc=0xb7c3ddd0, stop=0, 
stop_argument=0x0) at unwind.inc:211
#8  0xb7e4301a in _Unwind_ForcedUnwind () from /lib/tls/libpthread.so.0
#9  0xb7e411c3 in __pthread_unwind () from /lib/tls/libpthread.so.0
#10 0xb7e3ff33 in pthread_testcancel () from /lib/tls/libpthread.so.0
#11 0x08048ecc in thread_function (arg=0x0) at mt.C:166
#12 0xb7e3cb63 in start_thread () from /lib/tls/libpthread.so.0
#13 0xb7d1718a in clone () from /lib/tls/libc.so.6

Note that if one uses new(std::nothrow) version, the program works fine..
Of course in this case I can use nothrow version however this is only a small 
piece of code and I need to understand the roots of this behavior otherwise I 
cannot work around it in a  real program..

I know this has to deal with an exception mechanism in gcc, as synchronous 
cancellation is seemingly processed  as c++ exception..
I also tried to compile the program with -fno-exceptions flag and it works..

I use gcc 3.4.5
gcc -v:
--enable-shared --enable-threads=posix --enable-__cxa_atexit 
--with-system-zlib --enable-nls --without-included-gettext 
--enable-clocale=gnu --enable-debug

and compiled my program with -pthread flag.
below is the code:

class BigBug { 
public:
  BigBug() {
	x = new(std::nothrow) int;
	//x = new int; // uncomment this to get an error
  }
    
  ~BigBug() {
	delete x;
  }

  int *get_x() {
    return x;
  }
private:
   int *x;
};

void* thread_function (void*)
{
	BigBug *aa = new(std::nothrow) BigBug;
        //BigBug *aa = new BigBug; // uncomment this to get an error
	aa->get_x();
	delete aa;
	
	while(1) { 
	  pthread_testcancel();
	}
	return NULL;
}

int main ()
{
pthread_t thread;

pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

pthread_create (&thread, &attr, &thread_function, (void *)NULL);

sleep(2);
puts("cancelling thread..\n");
pthread_cancel(thread[0]);
sleep(1);

return 0;
}

best regards,
  Pavel



More information about the Gcc mailing list