هل يمكنني التراجع أو إزالة أمر atexit؟
سؤال
إذا وضعت atexit( fn );
في مكدس الخروج ، سيتم تنفيذه عندما يخرج البرنامج: يعود من main()
أو عن طريق exit()
.
هل يمكنني إزالته من المكدس؟
لماذا أريد أن أفعل هذا ، تسأل؟
كنت أجرب آلية محاولة بسيطة باستخدام atexit
, setjmp
و longjmp
. سيكون مثاليًا لو استطعت undo-atexit(fn);
- حتى لو كان سيعمل فقط في آخر وظيفة مسجلة.
يحرر:
بعد اقتراح Monoceres لجعل مكدس الخاص بي ...
المكدس يعمل فقط مع استثناء واحد الماسك في الوقت الحالي.
void (*_catchFn[10])() = {0,0,0,0,0,0,0,0,0,0};
void _catch(){
if ( _catchFn[0] != 0 ){
(_catchFn[0])();
}
}
void _addCatch( void (*fn)() ){
_catchFn[0]=fn;
}
void _remCatch( void (*fn)() ){
_catchFn[0]=0;
}
void test(){
jmp_buf env;
void catch(){ // we get here after an exit with a registered catch
longjmp(env,1); // return to the line marked except...
// that first will get the value 1
}
int first = setjmp( env); // ** return here **
fprintf( stderr , "test: After setjmp. first=%d\n" , first );
if( first == 0 ){ // try this code
_addCatch(catch); // register the catch function to 'catch' the exit
fprintf( stderr , "test: Before CHECK\n" );
// CHECK something and something bad happens and it exits
exit(1); // like this
fprintf( stderr , "test: After CHECK - THIS SHOULD NEVER BE SEEN AFTER AN EXCEPTION.\n" );
}else{
fprintf( stderr , "test: After longjmp return. first=%d\n" , first );
}
_remCatch( catch);
fprintf( stderr , "test: IT WORKED!\n");
exit(1); // exit again to see if we are safe
}
int main(){
atexit( _catch ); // register my global exception stack
test();
}
المحلول
لماذا لا تبني المكدس الخاص بك الذي تتصل به من وظيفة atexit () واحدة؟ بهذه الطريقة يمكنك معالجة المكدس كل ما تريد.
نصائح أخرى
لا ، لا يمكنك القيام بذلك ، ولكن يمكنك استخدام العلم العالمي حتى لا يفعل معالج الخروج أي شيء إذا تم تعيين العلم.
بدلاً من ذلك يمكنك الاتصال _Exit()
(C99) - سيؤدي إجراء مخرج طبيعي (أغلق جميع الواصفات المفتوحة ، وإرسال جميع الإشارات المطلوبة والأولياء/الأطفال) ولكن لن يتصل بمعالج الخروج.