fix a memory bug caused by R_alloc and c free