Fixed a resource leak in error case