[fix] properly free the allocated resources