Fix gif memory leak on error