質問

プログラム終了終了スタック上のIプレースatexit( fn );場合は、それが実行されます:。main()からかexit()を経由して戻り

私は、スタックからそれを削除できますか?

なぜあなたは尋ねる、私はこれをしたいですか?

私はatexitsetjmplongjmpを使用して簡単なのtry-catchメカニズムを実験しました。私はundo-atexit(fn);ができればそれだけで完璧になる - でも、それは最後の登録機能のためだけに働くだろう場合は、

編集ます:

私自身のスタックを作るには、以下のmonoceres'の提案...

スタックは今だけのための1つの例外キャッチャーで動作します。

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()関数から呼び出すこと、独自のスタックを構築していませんか?そうすれば、あなたはすべてのあなたが欲しいスタックを操作することができます。

他のヒント

いいえ、あなたはそれを行うことはできませんが、自分の終了ハンドラは何もしませんので、フラグが設定されている場合は、グローバルフラグを使用することができます。

別の方法としては、(C99)を_Exit()を呼び出すことができます - 。(すべての必要な信号と親/子どもを送っ近く開いているすべての記述子を)それは通常の終了手順を実行しますが、終了ハンドラを呼び出すことはありません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top