You can use RAII and put the cudaMalloc() and cudaFree() calls in the constructor and destructor of your object, respectively.
After the exception, the destructor will be called, which will free the allocated memory.
If you transfer this object to a smart pointer (or make it behave like a pointer), you will get your CUDA smart pointer.
source share