[fix] fix memory leaks on api function exit