我们能否取消使用atexit()注册的退出处理程序?

5

是否有可能取消注册退出处理程序函数?

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

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

1
不,这是不可能的。 - n. m.
@mata - 它被标记为C而不是Python。 - Ed Heal
3个回答

6

这是不可能的。

为什么不只注册一个 atexit 函数,并有一个全局变量来决定该函数需要执行什么操作呢?


2

您无法注销atexit函数,但可以禁用您自己的函数。

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;
}

0

atexit 允许一个函数被多次注册。为了注销一个函数,不存在的 unregister_atexit 不仅需要允许注销函数,还需要允许指定注销的位置。这很快就会暴露出用于存储由 atexit 注册的函数的堆栈接口。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接