Fix memory leak on error path