Fix memory leak on exception