fix potential memory leak when allocation fails