_penter function causes acces violation when using /Gh

Sándor Nyika 1 Reputation point
2021-10-23T19:23:49.393+00:00

Hi!

I'm trying to instrument a multithreaded program using the /Gh compiler option, but I ran into this interesting error. I wrote a simple DLL based on the Interceptor library from PrakaramJoshi, but when atexit() is called Access Violation Exception is thrown in _peneter.

I made a small code to reproduce the problem, here are my remarks:

  • The problem occurs when the instrumented program uses static global variables.
  • When compiling for debug the problem does not occur.
  • When choosing /EHs exception handling model the problem does not occur with global mutex.
  • But with /EHs and a global std::vector it is.

This is my project:
Interceptor-Access_Violation

I think this has something to do with static initialization. So my question is how should I solve this problem? Is there any other approach to dodge this?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,527 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2021-10-24T11:14:07.897+00:00

    Probably it tries executing some unloaded or uninitialised code. If you are not interested in _penter that are invoked during finalisation, you can apply this workaround:

    int main( )
    {
       . . .
       __declspec( dllimport ) bool exiting;
       exiting = true;
    
       return 0;
    }
    

    In Interceptor.h:

    __declspec( dllexport ) bool exiting = false;
    
    extern"C" void on_enter( const void* pa )
    {
       if( exiting ) return;
    
       . . .
    }
    
    0 comments No comments