问题
If not, what is the standard way to free up cudaMalloc
ed memory when an exception is thrown? (Note that I am unable to use Thrust.)
回答1:
You can use RAII idiom and put your cudaMalloc()
and cudaFree()
calls to the constructor and destructor of your object respectively.
Once the exception is thrown your destructor will be called which will free the allocated memory.
If you wrap this object into a smart-pointer (or make it behave like a pointer) you will get your CUDA smart-pointer.
来源:https://stackoverflow.com/questions/16509414/is-there-a-cuda-smart-pointer