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