Can we deregister an exit handler that has been registered with atexit()

StackOverflow https://stackoverflow.com/questions/23167035

  •  06-07-2023
  •  | 
  •  

문제

is it possible to unregister an exit handler function???

void exit_handler_1()
{
    printf("in first exit handler\n");
}

int main()
{
    if(atexit(exit_handler_1())
    {
        perror("error");
    }
    return 0;
}
도움이 되었습니까?

해결책

It is not possible.

Why not just register one atexit function and have a global variable for that function to be able to decide what is required of it.

다른 팁

You can't unregister atexit functions, but you can disable your own functions.

static int disable_my_exit_handler = 0;

void exit_handler_1()
{
    if ( disable_my_exit_handler )
        return;

    printf("in first exit handler\n");
}

int main( void )
{
    if ( atexit( exit_handler_1 ) )
    {
        perror("error");
    }

    disable_my_exit_handler = 1;
    return 0;
}

atexit allows a function to be registered multiple times too. In order to unregister a function, the non-existing unregister_atexit has to allow not only the function but also the position from which you want to unregister it. This quickly leads to exposing the interface of the stack used to keep store the functions registered by atexit.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top