Fix memory leak when handling error.