Free memory after THCudaTensor_maskedSelect?

Recently, I implemented a module using PyTorch C API. In my module, I need to repeatedly call THCudaTensor_maskedSelect in a for-loop.

THCudaTensor temp = THCudaTensor_new(state);
for (int i = 0; i < k; i++) {
THCudaTensor_maskedSelect(state, temp, input, mask);

}

It seems that THCudaTensor_maskedSelect will allocate memory for temp depending on the mask. Do I need to free the memory in every loop?

Only methods prefixed with new will need to be paired with methods prefixed with free.