Freeing tmpObject only once instead of n times