Search code examples
c++cuda

How to properly free a Cuda context?


I am implementing Optix denoising inside my C++ path tracer. I then need to create a Cuda context before calling Optix kernels. That context should be created every time i spawn a rendering thread since each thread have its own Cuda context

According to the Optix sample, Cuda is initialized with that piece of code:

CudaFree(nullptr)

Then i need to free this before my thread exits. How to do so? Or there is nothing that needs to be done?


Solution

  • cudaFree(nullptr) frees a non-existing pointer. Per the CUDA runtime documentation, no action is taken when a null pointer is freed:

    If devPtr is 0, no operation is performed.

    Therefore, no action is required on your end. Thanks!