Call destroy functions on error. (#531)

Fix leak on error path.