Fix memory leak on error