Memory leak due to a Tensor created with THCudaTensor_newWithSize2d in an extension

Hi,

I implemented a C extension to pytorch, in which I create the output tensor through
the TH_API THCudaTensor *THCudaTensor_newWithSize2d function, declared in THTensor.h.
This is then used in pytorch through the cffi.

In the end, I am using this function many times, overriding my variable.

Imagine something like

While True:
   result = myfunction(param1, parma2)
   # do stuff

It turns out that I have a memory leak: my CUDA memory is soon saturated.

Do I need to explicitly call some function to free that tensor ? It doesn’t seem like it’s freed.

Is the good practice to create a Python wrapper to that function, in which I define the output tensor, to be filled by this external module ? Or is there a way to do the allocation there as I do currently, leaving pytorch free this memory somehow ?

Thanks a lot

Answering to my own question:

it seems the correct way to go is indeed to take the result tensor as a parameter to the CUDA extension, and not to allocate this result tensor there.

Indeed, the corresponding memory is never released if the tensor has not been created in Python