fixed memory leak in error case