fixed memory leak in case of errors in alloc