Fix memory leak on failure