free allocated resources on error