Fixed a leak with releasing resources after certain exceptions