4 Mayıs 2016 Çarşamba

std::abort

Giriş
std::abort() Linux ve Windows dünyasındaki sinyallere değindiği için bana hep çok itici geliyor. Bu metodu kullanmamak gerektiğini düşünüyorum.

Kullanmak için şöyle yaparız.
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  printf("bla bla bla\n");
  abort();
}

Windows
Eğer uygulama console uygulaması ise şöyle bir çıktı alırız.
This application has requested the Runtime to terminate it in an unusual
way. Please contact the application's support team for more information.
Eğer uygulama GUI uygulaması ise şöyle bir mesaj görürürüz.
... has stopped working -- A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available.
Windows'ta abort çağrısını durdurmanın bir yolu var mı bilmiyorum.

Linux
Console uygulamasında şöyle bir çıktı alırız.
Aborted
Bu metod çağırılınca SIGABRT sinyali gönderiliyor. Uygulama SIGABRT sinyalini dikkate almamak için kod yazmışsa bile abort bir şekilde default handler'ı çağırıyor.
If the SIGABRT signal is ignored, or caught by a handler that returns, the abort() function will still terminate the process. It does this by restoring the default disposition for SIGABRT and then raising the signal for a second time.
Netice de SIGABRT için signal handler atansa bile, uygulamayı kesinkes kapatıyor.

abort handler
Şöyle bir kod yazılabilir. Ama handler içinde ne yapılabilir bilmiyorum.
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>

void handle_abort(int sig)
{
   // do something (e.g write to log)

}

int main(void)
{    
    signal(SIGABRT, handle_abort);

    abort();
}
assert
assert makrosu ile de std::abort() metodunu çağırır 


Hiç yorum yok:

Yorum Gönder