When you have several lines of codes that initiate other objects, is there a cleaner way to clean up the objects than what is shown below? I initiate several objects in one function and check to see if they fail- but I have a bunch of redundant code that I have to keep typing. Is what shown below the correct method of doing this? Or are there cleaner ways? I am aware of the do { } while(false) method and the goto method- but they are not clean and feel messy.
if( bind(s, (sockaddr *)&saddr, sizeof(sockaddr)) == SOCKET_ERROR ) {
printf("bind() failed.\n");
closesocket(s);
CloseHandle(g_hIOCompletionPort);
CloseHandle(g_hShutdownEvent);
WSACleanup();
}
if( listen(s, 60) == SOCKET_ERROR ) {
printf("listen() failed.\n");
closesocket(s);
CloseHandle(g_hIOCompletionPort);
CloseHandle(g_hShutdownEvent);
WSACleanup();
}
g_hAcceptEvent = WSACreateEvent();
if( g_hAcceptEvent == WSA_INVALID_EVENT ) {
printf("WSACreateEvent() failed.\n");
closesocket(s);
CloseHandle(g_hIOCompletionPort);
CloseHandle(g_hShutdownEvent);
WSACleanup();
}