fix possible leak on error handling