Fix memory leak on thrown exception