Installing an SEH (structured exception handler) in every thread

Installing an SEH (structured exception handler) in every thread

Hi all,

I tried to install my own handler for structured exceptions in every thread using the following code:

/// Translates strucutred exceptions to C++ exceptions
void structuredExceptionTranslator(unsigned int code, _EXCEPTION_POINTERS *ep)/// Translates strucutred exceptions to C++ exceptions by throwing a
void structuredExceptionTranslator(unsigned int code, _EXCEPTION_POINTERS *ep)
{
  throw std::exception();
}

/// Installs a handler for structured exceptions in every thread.
class StructuredExceptionHandlerInstaller : public tbb::task_scheduler_observer
{
public:
  StructuredExceptionHandlerInstaller()
  {
    observe(true);
  }
  void on_scheduler_entry(bool is_worker)
  {
    if (is_worker)
      _set_se_translator(structuredExceptionTranslator);
  }
};

/// main program
int main(int argc, char *argv[])
{
  // set translator function in order to translate strucutred exceptions to C++ exceptions
  _set_se_translator(structuredExceptionTranslator);
  StructuredExceptionHandlerInstaller g_SEHHandler;

  // execute the main program in a guarded section in order to catch all exceptions and log them before exiting
  try
  {
    // do stuff, including using tbb::parallel_for
    return EXIT_SUCCESS;
  }
  catch (...)
  {
    // log the exception
    // return a failure code
    return EXIT_FAILURE;
  }
}

However, some structured exceptions originating from inside parallel_for loops are still not handled by my custom SEH. This seems to be the case whenever I generate a parallel_for loop with more iterations than CPUs and with the context tbb::task_group_context(tbb::task_group_context::isolated) given to parallel_for. I am guessing that my handler does not get installed for all threads but I have no clue why that could be the case.

On the other hand, even if my SEH is called, it sometimes ends up failing an assertion:

Assertion p >= 0 && p < num_priority_levels failed on line 103 of file z:\itt\branch_tbb41\tbb\1.0\s
rc\tbb\scheduler_common.h

I found the related piece of code online:
inline void assert_priority_valid ( intptr_t& p ) {    __TBB_ASSERT_EX( p >= 0 && p < num_priority_levels, NULL );}
But again, I have no idea as to why this happens.

I'd be greatful to any hints, esp. since I am a novice w.r.t. TBB.

Thanks,
Andreas.

2 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.

I have devised a more simple and self contained example and posted it here: http://ideone.com/2kgMZu

The issue I have is that I do not deterministically get the output "seh exception caught". Sometimes the program simply crashes without catching any exceptions, i.e. I experience the old school behavior for SEH exceptions.

I tested with Visual Studio 2012, VC11 Update 3 and TBB 4.1 Update 3 (and 4, also the commercial version). The compiler switch /EHa is enabled and I am working with 64bit executables.

Any suggestions what I might be doing wrong would be highly welcome.

Regards, Hauke

p.s. The last post has been submitted by a colleague of mine with my consent and I hope that we did not violate any forum rules.

Connectez-vous pour laisser un commentaire.