S
Siemel Naran
(1) About std::exit. If I call this function, will the system call the
destructors of all objects in the call stack. Eg.
void f() { std::exit(1); }
void g() { X x; f(); }
Does the call in f() to std::exit invoke x.~X() in scope g()?
(2) About signals and exceptions. Is it possible to throw an exception from
a signal handler. On my compiler the program ends with the error in a
dialog box:
Project.exe faulted with message "application-defined exception". Process
Stopped. Use Step or Run to continue.
(3) So assuming that we can throw exceptions in signal handlers and that
exit does cleanup, is the following a good way to invoke cleanup when
either we exit normally, or throw an exception, or have a signal? My
instinct says something is wrong here:
int global_s = -1;
void closesockets(int sig)
{
if (global_s != -1)
{
closesocket(global_s);
global_s = -1;
}
if (sig) std::exit(sig);
}
class CloseSockets
{
public:
CloseSockets() { }
~CloseSockets() { closesockets(0); }
private:
CloseSockets(const CloseSockets&); // not implemented
CloseSockets& operator=(const CloseSockets&); // not implemented
};
void server()
{
CloseSockets _closesockets;
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1) throw std::runtime_error("fail socket");
global_s = s;
std::signal(SIGINT , &closesockets); // are these even necessary?
std::signal(SIGTERM, &closesockets);
std::signal(SIGABRT, &closesockets);
sockaddr_in my_address;
my_address.sin_family = AF_INET;
my_address.sin_port = htons(1972);
my_address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
std::memset(my_address.sin_zero, 0, sizeof(my_address.sin_zero));
int b = bind(s, convert(&my_address), sizeof(sockaddr));
if (b == -1) throw std::runtime_error("fail bind"); // will call
_closesocket's destructor
int l = listen(s, 10);
if (l == -1) throw std::runtime_error("fail listen"); // will call
_closesocket's destructor
while (true)
{
...
}
// will call _closesocket's destructor
}
destructors of all objects in the call stack. Eg.
void f() { std::exit(1); }
void g() { X x; f(); }
Does the call in f() to std::exit invoke x.~X() in scope g()?
(2) About signals and exceptions. Is it possible to throw an exception from
a signal handler. On my compiler the program ends with the error in a
dialog box:
Project.exe faulted with message "application-defined exception". Process
Stopped. Use Step or Run to continue.
(3) So assuming that we can throw exceptions in signal handlers and that
exit does cleanup, is the following a good way to invoke cleanup when
either we exit normally, or throw an exception, or have a signal? My
instinct says something is wrong here:
int global_s = -1;
void closesockets(int sig)
{
if (global_s != -1)
{
closesocket(global_s);
global_s = -1;
}
if (sig) std::exit(sig);
}
class CloseSockets
{
public:
CloseSockets() { }
~CloseSockets() { closesockets(0); }
private:
CloseSockets(const CloseSockets&); // not implemented
CloseSockets& operator=(const CloseSockets&); // not implemented
};
void server()
{
CloseSockets _closesockets;
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s == -1) throw std::runtime_error("fail socket");
global_s = s;
std::signal(SIGINT , &closesockets); // are these even necessary?
std::signal(SIGTERM, &closesockets);
std::signal(SIGABRT, &closesockets);
sockaddr_in my_address;
my_address.sin_family = AF_INET;
my_address.sin_port = htons(1972);
my_address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
std::memset(my_address.sin_zero, 0, sizeof(my_address.sin_zero));
int b = bind(s, convert(&my_address), sizeof(sockaddr));
if (b == -1) throw std::runtime_error("fail bind"); // will call
_closesocket's destructor
int l = listen(s, 10);
if (l == -1) throw std::runtime_error("fail listen"); // will call
_closesocket's destructor
while (true)
{
...
}
// will call _closesocket's destructor
}