When the close function returns, all resources should be freed, regardless of whether an error has occured or not. The error code is purely informative. However, in order to return the first error code, which is usually the most interesting one, the current implementation is unnecessary complicated. If an error occurs, there is no need to exit immediately. Simply store the error code unless there is already a previous one, and then continue.